Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

oracle

Say there is a variable

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

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

like image 663
user3616462 Avatar asked Dec 02 '22 14:12

user3616462


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;
like image 95
Sachu Avatar answered Jan 05 '23 16:01

Sachu


SELECT REGEXP_COUNT( 'stack#over#flo#w', '#' )
FROM   DUAL
like image 32
MT0 Avatar answered Jan 05 '23 16:01

MT0