我有一个这样的字符串:
'1,A;2,B;3,C'
无论如何,我可以将逗号分隔值拆分为列,然后按;
分割成行,如下所示:
ID Text
1 A
2 B
3 C
答案 0 :(得分:2)
试试这个:
declare @s varchar(50) = '1,A;2,B;3,C'
--convert string to xml table (I used HTML tags for clarity)
declare @xml xml = cast('<tr><td>' + replace(replace(@s, ';', '</td></tr><tr><td>'), ',', '</td><td>') + '</td></tr>' as xml)
--query the xml to get SQL table
select tbl.col.value('td[1]', 'int') [ID],
tbl.col.value('td[2]', 'varchar(10)') [Text]
from @xml.nodes('/tr') tbl(col)
答案 1 :(得分:0)
DECLARE @tags NVARCHAR(400) = '1,A;2,B;3,C'
SELECT SUBSTRING(value, 1, CHARINDEX(',',value)-1) AS ID
, SUBSTRING(value, CHARINDEX(',',value)+1, LEN(value)) AS TEXT
FROM(
SELECT value
FROM STRING_SPLIT(@tags, ',')
WHERE RTRIM(value) <> ''
) A