Multiple row Json value to Row

Mohamed Farook 161 Reputation points
2021-07-23T10:26:38.26+00:00

HI,
create table #temp (ID int,NameList nvarchar(max),Sort int)

insert into #temp (ID,NameList,Sort) VALUES (1001,'[{"User":"AAA"},{"User":"BBB"},{"User":"CCC"},{"User":"DDD"}]',1)
insert into #temp (ID,NameList,Sort) VALUES (1002,'[{"User":"EEE"}]',2)
insert into #temp (ID,NameList,Sort) VALUES (1002,'[{"User":"FFF"},{"User":"GGG"}]',3)

--Select

drop table #temp

i want result like below screen shot.

117368-image.jpg

SQL Server | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Viorel 125.7K Reputation points
    2021-07-23T10:32:23.173+00:00

    Try this:

    select ID, json_value([value], '$.User') as NameList, Sort
    from #temp
    cross apply openjson(NameList)
    order by ID, [key]
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.