0

我有一个表格,其中一列有逗号分隔值,我想要在行中..

喜欢

col1
3,4,5
5,6,6

返回结果应该是

col1
3
4
5
5
6
6
4

3 回答 3

2
    declare @tbl table(data nvarchar(100))
    insert into @tbl values('1,2,3')
    insert into @tbl values('4,5,6')
    insert into @tbl values('7,8,9')

     SELECT   
     Split.a.value('.', 'VARCHAR(100)') AS String  
     FROM  (SELECT data,  
         CAST ('<M>' + REPLACE(data, ',', '</M><M>') + '</M>' AS XML) AS String  
     FROM  @tbl) AS A CROSS APPLY String.nodes ('/M') AS Split(a);  
于 2012-12-11T10:13:22.543 回答
0

You can use a recursive query to perform this split:

;with cte (item, list) as
(
  select 
    cast(left(col1, charindex(',',col1+',')-1) as varchar(50)) item,
         stuff(col1, 1, charindex(',',col1+','), '') list
  from yourtable
  union all
  select 
    cast(left(list, charindex(',',list+',')-1) as varchar(50)) item,
    stuff(list, 1, charindex(',',list+','), '') list
  from cte
  where list > ''
) 
select item
from cte

See SQL Fiddle with Demo

于 2012-12-11T10:18:35.030 回答
0

我相信下面解释了如何遍历逗号分隔值。您可以将它们插入另一个变量以获得所需的输出。

逗号分隔值的拆分

于 2012-12-11T09:58:38.517 回答