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

SqlStringPostgresql

Sql Problem Overview


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


Example:

I have a table

CREATE TABLE test."user"
(
  uid integer NOT NULL,
  name text,
  result integer,
  CONSTRAINT pkey PRIMARY KEY (uid)
)

I want to write a query so that the result contains column how many occurrences of the substring o the column name contains. For instance, if in one row, name is hello world, the column result should contain 2, since there are two o in the string hello world.

In other words, I'm trying to write a query that would take as input:

enter image description here

and update the result column:

enter image description here


I am aware of the function regexp_matches and its g option, which indicates that the full (g = global) string needs to be scanned for the presence of all occurrences of the substring).

Example:

SELECT * FROM regexp_matches('hello world', 'o', 'g');

returns

{o}
{o}

and

SELECT COUNT(*)  FROM regexp_matches('hello world', 'o', 'g');

returns

2

But I don't see how to write an UPDATE query that would update the result column in such a way that it would contain how many occurrences of the substring o the column name contains.

Sql Solutions


Solution 1 - Sql

A common solution is based on this logic: replace the search string with an empty string and divide the difference between old and new length by the length of the search string

(CHAR_LENGTH(name) - CHAR_LENGTH(REPLACE(name, 'substring', ''))) 
/ CHAR_LENGTH('substring')

Hence:

UPDATE test."user"
SET result = 
	(CHAR_LENGTH(name) - CHAR_LENGTH(REPLACE(name, 'o', ''))) 
	/ CHAR_LENGTH('o');

Solution 2 - Sql

A Postgres'y way of doing this converts the string to an array and counts the length of the array (and then subtracts 1):

select array_length(string_to_array(name, 'o'), 1) - 1

Note that this works with longer substrings as well.

Hence:

update test."user"
    set result = array_length(string_to_array(name, 'o'), 1) - 1;

Solution 3 - Sql

Other way:

UPDATE test."user" SET result = length(regexp_replace(name, '[^o]', '', 'g'));

Solution 4 - Sql

Return count of character,

 SELECT (LENGTH('1.1.1.1') - LENGTH(REPLACE('1.1.1.1','.',''))) AS count
--RETURN COUNT OF CHARACTER '.'

Solution 5 - Sql

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.

Solution 6 - Sql

SELECT array_length(string_to_array('a long name here', 'o'),1)
  • 1 is for 1-dimension array
  • 'o' is the occurrence you want to count

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFranck DernoncourtView Question on Stackoverflow
Solution 1 - SqldnoethView Answer on Stackoverflow
Solution 2 - SqlGordon LinoffView Answer on Stackoverflow
Solution 3 - SqlbnsonView Answer on Stackoverflow
Solution 4 - SqlGuilherme PassosView Answer on Stackoverflow
Solution 5 - SqlRobert BondyView Answer on Stackoverflow
Solution 6 - SqlsparkleView Answer on Stackoverflow