SQL Server:拼合数组[重复]

laik7k3q  于 2023-01-04  发布在  SQL Server
关注(0)|答案(2)|浏览(128)
    • 此问题在此处已有答案**:

Unnest Arrays in SQL Server(2个答案)
昨天关门了。
我有一个类似这样的表:

id        my_array
-------------------------
1         ["a", "b", "c"]

我想把它弄平,看起来像这样:

id             my_array
------------------------
1               "a"
1               "b"
1               "c"

我已经尝试了openjson()没有成功。

vuv7lop3

vuv7lop31#

试试这个:

SELECT  
      1  id, 
      value my_array 
FROM STRING_SPLIT(replace(replace('["a", "b", "c"]','[',''),']',''),',');
ipakzgxi

ipakzgxi2#

DECLARE @T AS TABLE(id INT,Field VARCHAR(200))

INSERT INTO @T
SELECT 1 ,'["a", "b", "c"]'
UNION
SELECT 2 ,'["x", "y", "z"]'

SELECT A.id,LTRIM(REPLACE(REPLACE(REPLACE(B.value,'"',''),'[',''),']','')) AS my_array
FROM @T A 
CROSS APPLY (SELECT * FROM STRING_SPLIT(Field,',')) B

相关问题