Weekly post on Oracle Web site (TIP for Week of December 13, 2004)

Source: Internet
Author: User
Oracle
Calculate the number of times a string appears in another string
Aui de la Vega, Phil
CREATE FUNCTION num_chars (instring VARCHAR2, Inpattern VARCHAR2)
return number
Is
COUNTER number;
Next_index number;
STRING VARCHAR2 (2000);
Pattern VARCHAR2 (2000);
BEGIN
COUNTER: = 0;
Next_index: = 1;
STRING: = LOWER (instring);
Pattern: = LOWER (Inpattern);
For I in 1. LENGTH (STRING) LOOP
IF (Length (pattern) <= Length (STRING)-next_index+1)
and (SUBSTR (string,next_index,length) = pattern) THEN
COUNTER: = counter+1;
End IF;
Next_index: = next_index+1;
End LOOP;
return COUNTER;
End;
Password Analysis:
The code is not very complex, the use of Next_index plus an increase, to instring, from the instring string of the first Next_index characters to intercept
Length (pattern) long string, if the intercepted string is equal to pattern, the calculator counter+1, in Pl/sql developer+oracle10+winnt
Try to succeed.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.