逗号分离的麻烦

时间:2015-08-11 07:47:15

标签: sql

在我的表格中,我有这样的数据。

roolno  
-----------------------------
V658, V628
A001
A001, V003, V005, Z040
A001, V003, Z040
A001, A002
A001, A002, A003
A001, A002, A003, A004
A001, A002, A003, A004, A006
A001, A002, A003, A004, A006, A013, A015, A047, A100
A001, A002, A003, A004, A006, NULL, A046
NULL, B154, ''
C296, '', '  ', NULL, G652

我想在一个单独的行中获得价值。

例如:这只是我采用的一行

A001, A002, A003, A004, A006, NULL, A046

应该变成:

A001, 
A002, 
A003, 
A004, 
A006, 
NULL,
A046

我已经检查了Stack Overflow上已有的很多帖子,但是没有给我正确的结果。

请帮忙。

1 个答案:

答案 0 :(得分:0)

您可以使用

 SELECT   
 Split.r.value('.', 'VARCHAR(100)') AS Data  
 FROM  
 (
 SELECT CAST ('<M>' + REPLACE(roolno, ',', '</M><M>') + '</M>' AS XML) AS Data  
 FROM  table1
 ) AS r CROSS APPLY Data.nodes ('/M') AS Split(r);

或者如果你想把where子句放在你可以像这样的

 SELECT   
 Split.r.value('.', 'VARCHAR(100)') AS Data  
 FROM  
 (
 SELECT CAST ('<M>' + REPLACE(roolno, ',', '</M><M>') + '</M>' AS XML) AS Data  
 FROM  table1 where roolno='A001, V003, Z040'
 ) AS r CROSS APPLY Data.nodes ('/M') AS Split(r);