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
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.