The query outputs lines with more than one comma, but I only need ONE comma and ONE semicolon
SELECT *
FROM table
WHERE history_coord is NULL
OR history_coord LIKE '%,%' AND history_coord LIKE '%;%'
How do I change the request? maybe I don’t have to use "like"?
3
Answers
Maybe this will help you :
In this query REGEXP pattern used to match one comma and one semicolon.
^
is start of string[^,]
is matches with any number of characters that are not comma.,
is matches with comma.;
is matches with semicolon.[^;]
is matches with any number if characters which are not semicolon.$
is end of the string.Straightforward, just as you said – capture commas and semicolons with regular expressions using
regexp_matches
and then count them.P.S.
If your PostgreSQL version is 15+ then use function
regexp_count
which is roughly equivalent to the scalar subqueries above.This is a way to do it by converting the string to an array using
string_to_array
and counts the length of the array usingarray_length
and then substract 1 to count the number of occurrences of a substring within a string :Simple data :
The query to get records with only one comma and one semi colon :
Result :
Demo here