Counting the number of occurrences of a substring within a string in PostgreSQL

后端 未结 5 988
南旧
南旧 2020-12-03 09:49

How can I count the number of occurrences of a substring within a string in PostgreSQL?


Example:

I have a table

CREATE TABLE test.\"use         


        
5条回答
  •  不思量自难忘°
    2020-12-03 10:05

    Occcurence_Count = LENGTH(REPLACE(string_to_search,string_to_find,'~'))-LENGTH(REPLACE(string_to_search,string_to_find,''))
    

    This solution is a bit cleaner than many that I have seen, especially with no divisor. You can turn this into a function or use within a Select.
    No variables required. I use tilde as a replacement character, but any character that is not in the dataset will work.

提交回复
热议问题