How to calculate the number of occurrences of a character in varchar in one SQL?

Say there is a variable

v_Source := 'stack#over#flo#w';

How to get the number of occurrences ' #' in it in one SQL query?

+4
source share
2 answers
select length('stack#over#flo#w') - length(replace('stack#over#flo#w','#',null)) 
from dual;

From oracle 11 you can use REGEXP_COUNT

select REGEXP_COUNT('stack#over#flo#w', '#') from dual;
+10
source
SELECT REGEXP_COUNT( 'stack#over#flo#w', '#' )
FROM   DUAL
+7
source

All Articles