PostgreSQL count number of times substring occurs in text

后端 未结 4 1865
名媛妹妹
名媛妹妹 2020-12-19 20:53

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\'

4条回答
  •  失恋的感觉
    2020-12-19 21:40

    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

提交回复
热议问题