How do I count the number of occurrences of a substring inside of a string?
As of version 11g, regexp_count
will do this.
select regexp_count('abba', 'b') from dual;
2
select regexp_count('abba', 'b+') from dual;
1
In pre-11g version you can:
select (length(string) - length(replace(string,substring,''))) / length(substring) as occ
from dual;
The idea is to see how much space in the string is occupied by the substring, then, to see how many times the substring is in that space just divide to the length of substring.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With