2012-03-14 7 views
3

하나의 열을 여러 행에 표시하는 방법을 찾고있었습니다. 쉼표로 구분 된 내용입니다. 대신에 예를 들어여러 행에 하나의 열 표시

:

ProjectID 라벨 ---- --- 1200 LABEL1 1200 라벨 2 1200 LABEL3

I는 다음과 같이 내 쿼리의 결과를 싶습니다

: 사전에

ProjectID     Label 
————     ——– 
1200       label1, label2, label3 

감사

+0

확인이 하나 http://stackoverflow.com/questions/5493510/turning-a-comma-separated-string-into-individualrows –

답변

3

이렇게하는 방법은 여러 가지가 있습니다. 한 가지 옵션은 다중 값 셀을 다른 레코드로 '분할'하는 테이블 값 함수를 만드는 것입니다.

ALTER FUNCTION [dbo].[Split](@RowData VARCHAR(MAX), @SplitOn VARCHAR(5)) 
RETURNS @RtnValue TABLE 
(
    Id int identity(1,1), 
    Data VARCHAR(MAX) 
) 
AS 
BEGIN 
    Declare @Cnt int 
    Set @Cnt = 1 

    While (Charindex(@SplitOn,@RowData)>0) 
    Begin 
     Insert Into @RtnValue (data) 
     Select 
      Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1))) 

     Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData)) 
     Set @Cnt = @Cnt + 1 
    End 

    Insert Into @RtnValue (data) 
    Select Data = ltrim(rtrim(@RowData)) 

    Return 
END 

생성되면, 당신은 당신의 결과를 얻기 위해 다음을 수행 할 수 있습니다 : 여기에 분할 기능의 예는 다음

SELECT * 
FROM YourTable A 
CROSS APPLY dbo.Split(Label,', ') B 
+0

'TABLE'결과에 ID를 추가하지 않으면 함수가 훨씬 빠르게 실행되고 함수에 SplitOn 매개 변수를 추가해야합니다. – Jaques

+0

@Jaques - Yeap, 네 말이 맞아, 내 대답을 업데이트했다. ID에 관해서는 함수의 속도가 느려지지만 결과의 순서가 중요 할 때 정말 유용하다는 것을 알게되었습니다. – Lamak

-1

테이블을 반환하는 분할 함수와 함께 SQL Server 테이블 함수 사용

+0

쿼리에서 도움이 필요합니다 .... – user960439

+3

'PIVOT '이 전혀 도움이되지 않습니다. – Lamak

2

, 나는 문자열과 수익을 분할 테이블 반환 함수를 만들었습니다 당신이 원하는대로 결과

--Create the function 
    CREATE FUNCTION dbo.Split(@ProjectId nvarchar(50),@String varchar(8000), @Delimiter char(1))  --Pass projectID,label and delimiter and returns table 
    returns @temptable TABLE (id nvarchar(50),items varchar(8000))  
    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(id,Items) values(@ProjectId,@slice)  

      set @String = right(@String,len(@String) - @idx)  
      if len(@String) = 0 break  
     end 
    return  
    end 
--Calling the function 
select * from dbo.split('1200',' label1, label2, label3',',') --calling teh function 
0
create table #comma_seprate 
(ProductID int, 
Lable varchar(max)) 

declare @index int, @id int; 
declare @lable varchar(max); 
declare cur_comma cursor 
for select ProductID, Lable from comma_seprate 
open cur_comma 
fetch next from cur_comma into @id, @lable 
while (@@fetch_status=0) 
begin 
    set @index=charindex(',',@lable); 
    while(@index>0) 
    begin 
     insert into #comma_seprate values (@id,rtrim(ltrim(left(@lable,@index-1)))); 
     set @lable=substring(@lable,@index+1,len(@lable)); 
     set @index=charindex(',',@lable); 
    end 
    insert into #comma_seprate values (@id, rtrim(ltrim(@lable))); 
    fetch next from cur_comma into @id,@lable; 
end 
close cur_comma; 
deallocate cur_comma; 
select * from #comma_seprate; 
truncate table #comma_seprate; 
관련 문제