How to print names in string list in a single row?
Suppose that I have data like this:
SELECT XName FROM X_Table --(table has other columns too)
XName
-------------
Foo
Bar
How to get values like this:
["foo", "bar"]
How to print names in string list in a single row?
Suppose that I have data like this:
SELECT XName FROM X_Table --(table has other columns too)
XName
-------------
Foo
Bar
How to get values like this:
["foo", "bar"]
I think you are looking at combination of JSON conversion and de-conversion syntax like below
Declare @js nvarchar(max)
SET @js= (SELECT XName FROM X_Table for json path)
select * from OPENJSON (@js) WITH ( Xname nvarchar(max) N'$.XName')
And if you want to avoid using a variable @js you can do
select * from OPENJSON
((SELECT XName FROM XTable for json path))-- note double parenthesis
WITH ( Xname nvarchar(max) N'$.XName')
From the example given in G.Stoynev link, you can do the following:
SELECT JSON_QUERY
(
(
SELECT JSON_QUERY
(
'[' + STUFF(( SELECT ',' + '"' + convert(varchar(10), XName) + '"'
FROM dbo.X_Table
FOR XML PATH('')),1,1,'') + ']'
) Categories
FOR JSON PATH , WITHOUT_ARRAY_WRAPPER
), '$.Categories'
)