0

I have been searching this topic and I am finding examples that deal with strings but not so much about arrays. I start out with a JSON string stored in a single column, looking like this...

query one

I then can use this query to extract just the Ids portion of each record...

SELECT Id, Message,
    JSON_QUERY(Message, 'strict$.Ids') AS Ids
FROM ActionQueue
WHERE Status = 1
    AND (Action = 'Approve' OR Action = 'Reject')

id results

I need to now combine those arrays (not sure if they are strings that look like arrays?) into a single array.

I need to use that list of Ids for passing to a stored procedure as well as getting a count of the Ids.

1 Answer 1

1
declare @t table(thejsoncolumn nvarchar(max));

insert into @t(thejsoncolumn)
values(N'{"Ids":[1, 2, 3, 4, 5]}'), (N'{"Ids":[7]}'), (N'{"Ids":[6, 9, 10, 11]}');

select stuff(
(select concat(',', value)
--string_agg(value, ',') within group (order by cast(value as int)) AS thelist
from
(
select distinct j.value
from @t as t
cross apply openjson(t.thejsoncolumn, '$.Ids') as j
) as src
order by cast(value as int)
for xml path('') --..numbers only
), 1, 1, '');
Sign up to request clarification or add additional context in comments.

1 Comment

Msg 195, Level 15, State 10, Line 6 'string_agg' is not a recognized built-in function name. Msg 156, Level 15, State 1, Line 12 Incorrect syntax near the keyword 'as'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.