0

I have one table like this.

 SQL> SELECT * FROM FRUIT;

 F_NAME
 ----------
 APPLE
 PPPALEP
 APLEE
 PPAALLEEPP
 ornPpfpP    
 PPdhppPP  

Above one is my source table and I want to below output.If i am giving 'P' in multiform like including capital and small both. I want to count only 'P' from each row.

OUTPUT
------
F_NAME      COUNT
------      -----
APPLE         2
PPPALEP       4
APLEE         1
PPAALLEEPP    4
ornPpfpP      4
PPdhppPP      6

Thanks in advance.

2 Answers 2

2

Oracle has the very convenient regexp_count(). So:

select f_name, regexp_count(f_name, 'P') as cnt
from fruit;
Sign up to request clarification or add additional context in comments.

2 Comments

i didn't know about this function. thanks. i see it was introduced in oracle 11g.
I have tried a lot using regexp_count,but it is not working in oracle 10g.
0

You can count the number of occurrences by replacing P with blanks and subtracting the length of the replaced string from the original string.

select f_name,length(f_name)-length(replace(f_name,'P','')) cnt
from fruit

Edit: Per OP's comment, to count both P and p, use upper or lower when replacing the character with an empty string.

select f_name,length(f_name)-length(replace(upper(f_name),'P','')) cnt
from fruit

3 Comments

This query is working fine for only count capital 'P' .If i am giving 'P' in multiform like including capital and small both. I want to count both 'P'(like capital and small) from each row.Then what is the logic.
Thanks in advance @vkp
Now i got my answer.Thanks @vkp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.