一个字段串拆分成多行显示或者多行数据合并成一个字符串
DECLARE @table1 TABLE ( ID INT , Col1 NVARCHAR(50) , Col2 NVARCHAR(50) ); INSERT INTO @table1 VALUES ( 1, 'a,b,c', '诶,必,塞,地,伊' ); INSERT INTO @table1 VALUES ( 2, 'w', N'三四,不知道咧' ); --方式一 SELECT a.ID , a.Col1 , a.Col2 , v1 , v2 FROM ( SELECT ID , Col1 , Col2 , CONVERT(XML, '<n>' + REPLACE(REPLACE(Col1, ',', ','), ',', '</n><n>') + '</n>') AS xmlval1 , CONVERT(XML, '<n>' + REPLACE(REPLACE(Col2, ',', ','), ',', '</n><n>') + '</n>') AS xmlval2 FROM @table1 ) a CROSS APPLY ( SELECT k.n.value('.', 'nvarchar(80)') v1 FROM a.xmlval1.nodes('n') k ( n ) ) bs CROSS APPLY ( SELECT k.n.value('.', 'nvarchar(80)') v2 FROM a.xmlval2.nodes('n') k ( n ) ) ns; --方式二 SELECT ID , t.Col1 , t.Col2 , v1 , v2 FROM @table1 AS t CROSS APPLY ( VALUES ( CONVERT(XML, '<n>' + REPLACE(REPLACE(Col1, ',', ','), ',', '</n><n>') + '</n>'), CONVERT(XML, '<n>' + REPLACE(REPLACE(Col2, ',', ','), ',', '</n><n>') + '</n>')) ) a ( xmlval1, xmlval2 ) CROSS APPLY ( SELECT k.n.value('.', 'varchar(80)') AS v1 FROM a.xmlval1.nodes('n') k ( n ) ) bs CROSS APPLY ( SELECT k.n.value('.', 'varchar(80)') AS v2 FROM a.xmlval2.nodes('n') k ( n ) ) ns;
转自:https://www.cnblogs.com/springsnow/p/10289104.html