Using SQL Server 2022, I'm trying to figure out how to use STRING_SPLIT
in a set-based way to convert a table column where all rows have a variable number of key/value pairs into a table without duplicates, similar to T-SQL split string but not quite.
create table #mashed (mashedtext nvarchar(1600))
insert into #mashed
values ('"id":"one", "code":"aaa", "dev":"yes"')
insert into #mashed
values ('"id":"two", "code":"bbb", "dev":"yes"')
insert into #mashed
values ('"id":"three", "code":"ccc", "dev":"no"')
insert into #mashed
values ('"id":"three", "code":"bbb", "help":"no" , "rid":"6"')
Desired shape of output
key value
----------------
id one
id two
id three
code aaa
code bbb
code ccc
dev yes
dev no
help no
rid 6
This is clearly wrong:
SELECT value
FROM STRING_SPLIT (SELECT mashedtext FROM #mashed, ',')
So what is the right way to do this without RBAR? I am sure there are two splits needed, once to get the pairs and again on each pair.
Thanks.
2条答案
按热度按时间wwtsj6pe1#
The stored text data is almost a valid JSON object, so you may try to fix it and parse it with
OPENJSON()
:If you want to use
STRING_SPLIT()
you need to use the third optional parameter inSTRING_SPLIT()
(introduced in SQL Server 2022) to get the ordinal position of the each substring:Result (without order):
v8wbuo2f2#
STRING_SPLIT()
to split into rows and then perform string parsing to extract the requiredkey
andvalue