Given a name_loc column of text like the following:
{"Charlie – White Plains, NY","Wrigley – Minneapolis, MN","Ana – Decatur, GA"}
I’m trying to extract the names, ideally separated by commas:
Charlie, Wrigley, Ana
I’ve gotten this far:
SELECT SUBSTRING(CAST(name_loc AS VARCHAR) from '"([^ –]+)')
FROM table;
which returns
Charlie
How can I extend this query to extract all names?
3
Answers
You can do this with a combination of
regexp_matches
(to extract the names),array_agg
(to regroup all matches in a row) andarray_to_string
(to format the array as you’d like, e.g. with a comma separator):View on DB Fiddle
My two cents, though I’m rather new to postgreSQL and I had to copy the 1st piece from @Marth’s his answer:
Your string literal happens to be a valid array literal.
(Maybe not by coincidence? And the column should be type
text[]
to begin with?)If that’s the reliable format, there is a safe and simple solution:
Or:
db<>fiddle here
Steps
Unnest the array with
unnest()
in aLATERAL CROSS JOIN
, or directly in theSELECT
list.Take the first part with
split_part()
. I chose' – '
as delimiter, not just' '
, to allow for names with nested space like "Anne Nicole". See:Aggregate results with
string_agg()
. I added no particular order as you didn’t specify one.