考虑一下我已经创建了dbo.split
函数,其运行方式如下:
查询:
select substr from dbo.split(',' , 'A,B,C')
结果:
|Substr|
--------
|A |
--------
|B |
--------
|C |
--------
现在我想使用此函数转换此表:
|ID|Char|
---------
|1 |A,B |
---------
|2 |A |
---------
|3 |B,C |
---------
到这个结果:
|ID|Char|
---------
|1 |A |
---------
|1 |B |
---------
|2 |A |
---------
|3 |B |
---------
|3 |C |
---------
答案 0 :(得分:2)
试试这个:
这没有Split
功能。
DECLARE @Table TABLE(ID INT, [Char] VARCHAR(20))
INSERT INTO @Table VALUES(1,'A,B')
INSERT INTO @Table VALUES(2,'A')
INSERT INTO @Table VALUES(3,'B,C')
SELECT A.ID,
Split.a.value('.', 'VARCHAR(100)') AS [Char]
FROM
(
SELECT ID,
CAST ('<M>' + REPLACE([Char], ',', '</M><M>') + '</M>' AS XML) AS Data
FROM @Table
) AS A CROSS APPLY Data.nodes ('/M') AS Split(a);
答案 1 :(得分:0)