Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 373
Franck Dernoncourt Avatar asked Apr 02 '16 17:04

Franck Dernoncourt


People also ask

How do you count the number of occurrences of a substring in a string?

count() Return Value count() method returns the number of occurrences of the substring in the given string.

How do I count strings in PostgreSQL?

CHAR_LENGTH() function The PostgreSQL char_length function or character_length function is used to count the number of characters in a specified string. The CHARACTER_LENGTH() function is similar to CHAR_LENGTH() function. A string whose length is to be retrieved.

What is count (*) in PostgreSQL?

The PostgreSQL COUNT function counts a number of rows or non-NULL values against a specific column from a table. When an asterisk(*) is used with count function the total number of rows returns. Syntax: COUNT (* | [DISTINCT] ALL | column_name)


1 Answers

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'); 
like image 124
dnoeth Avatar answered Sep 30 '22 20:09

dnoeth