2012-10-31 78 views
6

可能重複:
Turning a Comma Separated string into individual rows拆分值

我從存儲過程以下輸出,並想知道的價值觀分裂成多行的最佳方式。

reference name       subjects  subjectstitle 
LL9X81MT Making and Decorating Pottery F06,F27,F38  NULL 

我需要修剪逗號的主題字段,並複製三行信息,所以數據如下。

reference name       subjects  subjectstitle 
LL9X81MT Making and Decorating Pottery F06  NULL 
LL9X81MT Making and Decorating Pottery F27  NULL 
LL9X81MT Making and Decorating Pottery F38  NULL 

我正在使用MS SQL Server 2008來設置這些SP,只需要一些關於如何分割主題字段的幫助。

感謝,

+0

數據如何進入該格式?你能不能在第一時間正確地歸還它? – podiluska

回答

14

您將要使用某種類型的表值分割功能的類似:

create FUNCTION [dbo].[Split](@String varchar(MAX), @Delimiter char(1))  
returns @temptable TABLE (items varchar(MAX))  
as  
begin  
    declare @idx int  
    declare @slice varchar(8000)  

    select @idx = 1  
     if len(@String)<1 or @String is null return  

    while @idx!= 0  
    begin  
     set @idx = charindex(@Delimiter,@String)  
     if @idx!=0  
      set @slice = left(@String,@idx - 1)  
     else  
      set @slice = @String  

     if(len(@slice)>0) 
      insert into @temptable(Items) values(@slice)  

     set @String = right(@String,len(@String) - @idx)  
     if len(@String) = 0 break  
    end 
return 
end; 

然後可以使用outer apply加入與yourtable:

select t1.reference, 
    t1.name, 
    t1.subjectstitle, 
    i.items subjects 
from yourtable t1 
outer apply dbo.split(t1.subjects, ',') i 

給出如下結果:

| REFERENCE |       NAME | SUBJECTSTITLE | SUBJECTS | 
------------------------------------------------------------------------ 
| LL9X81MT | Making and Decorating Pottery |  (null) |  F06 | 
| LL9X81MT | Making and Decorating Pottery |  (null) |  F27 | 
| LL9X81MT | Making and Decorating Pottery |  (null) |  F38 | 

SQL fiddle with Demo

如果你想這樣做沒有分裂的功能,那麼你可以使用CTE:

;with cte (reference, name, subjectstitle, subjectitem, subjects) as 
(
    select reference, 
    name, 
    subjectstitle, 
    cast(left(subjects, charindex(',',subjects+',')-1) as varchar(50)) subjectitem, 
     stuff(subjects, 1, charindex(',',subjects+','), '') subjects 
    from yourtable 
    union all 
    select reference, 
    name, 
    subjectstitle, 
    cast(left(subjects, charindex(',',subjects+',')-1) as varchar(50)) , 
    stuff(subjects, 1, charindex(',',subjects+','), '') subjects 
    from cte 
    where subjects > '' 
) 
select reference, name, subjectstitle, subjectitem 
from cte 

SQL Fiddle with Demo

+0

+1:儘管如此,你可能會更好地使用「OUTER APPLY」,以防萬一...... – MatBailie

+0

@請問您可能是對的,謝謝您的更正。 – Taryn

+0

在這個borad上已經有了這個話題,看起來像解決了那裏 http://stackoverflow.com/questions/5493510/turning-a-comma-separated-string-into-individual-rows – Jester

9

這將在不分裂的功能做

SELECT T1.reference, T1.name, T2.my_Splits AS subjects, T1.subtitile 
FROM 
(
    SELECT *, 
    CAST('<X>'+replace(T.subjects,',','</X><X>')+'</X>' as XML) as my_Xml 
    FROM [yourTable] T 
) T1 
CROSS APPLY 
( 
SELECT my_Data.D.value('.','varchar(50)') as my_Splits 
FROM T1.my_Xml.nodes('X') as my_Data(D) 
) T2 
+2

非常好。很棒。 – smoore4

+0

這太好了。是否有一種方法可以添加序列號以與分割值一起使用? – aaaantoine

+0

我想明白了。在T2的「my_splits」上面添加一個額外的列:row_number()over(order by my_data.D)as sequence_num,'''' – aaaantoine