I have the following json passed into a prameter in SQL server 2016:
[
{
"item_name": "Settee"
},
{
"item_name": "Television"
},
{
"item_name": "Chair"
}
]
How can I convert the item_name values into a comma separated string so that I get "Settee", "Television", "Chair"
which I can then insert into a table column?
This is what I have but that just lists them in rows:
select *
FROM OPENJSON(@json)
WITH (
item_name nvarchar(max) '$.item_name'
)
*** UPDATE ***
It's ok I've figured it out.
select items = stuff((
SELECT ', ' + j.item_name
FROM OPENJSON(@json)
WITH (
item_name nvarchar(max) '$.item_name'
) j FOR XML PATH('')
), 1, 1, '')