So pushing you text blob into a CTE.
with data as (
SELECT * FROM VALUES
('[{"entryListId": 3279,"id": 4617,"name": "SpecTra","type": 0},{"entryListId": 3279,"id": 7455,"name": "Signal Capital Partners","type": 0}]')
t(str)
)
I cannot help but note it's JSON, so lets PARSE_JSON that and then FLATTEN it, and here are you "names"
select
d.*
,f.value:name::text as name
from data d
,table(flatten(input=>parse_json(d.str))) f
giving:
| STR |
NAME |
| [{"entryListId": 3279,"id": 4617,"name": "SpecTra","type": 0},{"entryListId": 3279,"id": 7455,"name": "Signal Capital Partners","type": 0}] |
SpecTra |
| [{"entryListId": 3279,"id": 4617,"name": "SpecTra","type": 0},{"entryListId": 3279,"id": 7455,"name": "Signal Capital Partners","type": 0}] |
Signal Capital Partners |
And thus to aggregate, using LISTAGG
select
listagg(f.value:name::text, ',') as names
from data d
,table(flatten(input=>parse_json(d.str))) f
gives:
| NAMES |
| SpecTra,Signal Capital Partners |
Duplicate data:
you can add DISTINCT to the LISTAGG and have only the distinct values kept, but given it is a cost, I did point it out, and you didn't mention duplicate data.
with data as (
SELECT * FROM VALUES
('[
{
"entryListId": 3279,
"id": 4617,
"name": "SpecTra",
"type": 0
},
{
"entryListId": 3279,
"id": 4617,
"name": "SpecTra",
"type": 0
},
{
"entryListId": 3279,
"id": 7455,
"name": "Signal Capital Partners",
"type": 0
}]')
t(str)
)
select
listagg(distinct f.value:name::text, ',') as names
from data d
,table(flatten(input=>parse_json(d.str))) f;
gives:
| NAMES |
| SpecTra,Signal Capital Partners |
Where-as that regex solution does not handle this case:
with data as (
SELECT * FROM VALUES
('[
{
"entryListId": 3279,
"id": 4617,
"name": "SpecTra",
"type": 0
},
{
"entryListId": 3279,
"id": 4617,
"name": "SpecTra",
"type": 0
},
{
"entryListId": 3279,
"id": 7455,
"name": "Signal Capital Partners",
"type": 0
}]')
t(str)
)
select
trim(regexp_replace(regexp_replace(d.str, '"name":\\s*"([^"]+)"|.', '\\1,'), ',+', ','), ',') as regexp_replace
from data d
gives:
| REGEXP_REPLACE |
| , , , SpecTra, , , , , , SpecTra, , , , , , Signal Capital Partners, , |