I\'m writing a PostgreSQL function to count the number of times a particular text substring occurs in another piece of text. For example, calling count(\'foobarbaz\', \'ba\'
There is a
str_count( src, occurence )
function based on
SELECT (length( str ) - length(replace( str, occurrence, '' ))) / length( occurence )
and a
str_countm( src, regexp )
based on the @MikeT-mentioned
SELECT count(*) FROM regexp_matches( str, regexp, 'g')
available here: postgres-utils