any build-in function to count the appearences of a character in a string [message #2501] |
Sun, 21 July 2002 07:24 |
keane
Messages: 23 Registered: June 2002
|
Junior Member |
|
|
Hi,
I want to count how many '#' in string 'a#b#c#', so I do the following,
size := 1;
while INSTR('a#b#c#', '#', 1, size) > 0 LOOP
size := size + 1;
end loop;
size := size - 1;
I think it works. Do you know any build-in function can do it quickly? Or there are better ways to do it?
Thanks a lot.
keane
|
|
|
Re: any build-in function to count the appearences of a character in a string [message #2507 is a reply to message #2501] |
Mon, 22 July 2002 10:57 |
Todd Barry
Messages: 4819 Registered: August 2001
|
Senior Member |
|
|
The easiest way to to substract the length of the string with the character removed from the original length of the string. For example, to count the occurances of 'E':
sql>select object_type,
2 nvl(length(object_type), 0) -
3 nvl(length(replace(object_type, 'E')), 0) number_of_e
4 from all_objects
5 group by object_type;
OBJECT_TYPE NUMBER_OF_E
------------------ -----------
CONSUMER GROUP 1
CONTEXT 1
FUNCTION 0
INDEX 1
INDEXTYPE 2
JAVA CLASS 0
JAVA RESOURCE 2
LIBRARY 0
OPERATOR 1
PACKAGE 1
PROCEDURE 2
SEQUENCE 3
SYNONYM 0
TABLE 1
TYPE 1
VIEW 1
In PL/SQL, this would be:
v_size := nvl(length(v_string), 0) - nvl(length(replace(v_string, v_character)), 0);
|
|
|
|