2014-09-02 5 views
0

난 당신이 아이디어를 얻을 수있는이 예제를 사용하여 개별 열SQL Server 2008R2에서 단일 셀을 여러 열로 분할하는 방법?

create table split_test(value integer,Allnames varchar(40)) 
insert into split_test values(1,'Vinoth,Kumar,Raja,Manoj,Jamal,Bala'); 
select * from split_test; 

Value Allnames 
------------------- 
1  Vinoth,Kumar,Raja,Manoj,Jamal,Bala 

예상 출력

values N1  N2  N3  N4 N5  N6 N7.......N20 
1  Vinoth Kumar Raja Manoj Jamal Bala 
+0

이것은 매우 나쁜 디자인입니다. 테이블을 정상화하십시오. –

+0

검색 google을 위해 : sql server csv 행을 변환합니다. http://www.codeproject.com/Tips/732596/Converting-comma-separated-data-in-a-column-to-row –

+0

가능한 중복 [SQL Server 2005를 사용하여 쉼표로 구분 된 값을 별도의 행으로 어떻게 확장합니까? ?] (http://stackoverflow.com/questions/702968/how-do-i-expand-comma-separated-values-into-separate-rows-using-sql-server-2005) –

답변

0

각 이름을 분할 할.

declare @str varchar(max) 
set @str = 'Hello world' 

declare @separator varchar(max) 
set @separator = ' ' 

declare @Splited table(id int identity(1,1), item varchar(max)) 

set @str = REPLACE(@str,@separator,'''),(''') 

set @str = 'select * from (values('''[email protected]+''')) as V(A)' 

insert into @Splited 
exec(@str) 
select * from @Splited 
0

다음은 재귀 CTE를 사용하여 이름을 행으로 분리 한 다음 행을 열로 피벗하는 SQL 문입니다. SqlFiddle

with names as 
(select 
    value, 
    1 as name_id, 
    substring(Allnames,1,charindex(',',Allnames+',', 0)-1) as name, 
    substring(Allnames,charindex(',',Allnames, 0)+1, 40) as left_names 
from split_test 
union all 
select 
    value, 
    name_id +1, 
    case when charindex(',',left_names, 0)> 0 then 
    substring(left_names,1,charindex(',',left_names, 0)-1) 
    else left_names end as name, 
    case when charindex(',',left_names, 0)> 0 then 
    substring(left_names,charindex(',',left_names, 0)+1, 40) 
    else '' end as left_names 
from names 
where ltrim(left_names)<>'') 
select value, 
    [1],[2],[3],[4],[5],[6],[7],[8],[9] 
from (select value,name_id,name from names) as t1 
PIVOT (MAX(name) FOR name_id IN ([1],[2],[3],[4],[5],[6],[7],[8],[9])) AS t2 

UPDATE

@KM.'s answer는 재귀 CTE 테이블없이 행에 데이터를 분할하는 더 나은 방법이 될 수 있습니다. 이보다 더 효율적이어야합니다. 따라서이 예제를 따르고 null 값 프로세스 로직 부분을 단순화했습니다.

단계 1 : 테이블은 Allnames 열의 최대 길이보다 강판 번호 1에서 모든 숫자를 포함 만들기 다음의 결과이다. 2

CREATE TABLE Numbers(Number int not null primary key); 
with n as 
(select 1 as num 
union all 
select num +1 
from n 
where num<100) 
insert into numbers 
select num from n; 

단계 : 숫자 테이블과 split_test 테이블의 데이터를 가입, 우리는 모든 부분이 ,에서 시작 얻을 수 있습니다. 그런 다음 모든 행에서 2 , 사이의 첫 번째 부분을 가져옵니다. null 값이 있으면 union으로 추가하십시오.

select value , 
ltrim(rtrim(substring(allnames,number+1,charindex(',',substring(allnames,number,40),2)-2))) as name 
from 
(select value, ','+allnames+',' as allnames 
    from split_test) as t1 
left join numbers 
on number<= len(allnames) 
where substring(allnames,number,1)=',' 
and substring(allnames,number,40)<>',' 
union 
select value, Allnames 
from split_test 
where Allnames is null 

3 단계 : 생략 위 내 첫 번째 시도, 같은 열에 행에서 피벗 이름. SQLFiddle

+0

질문을 둘러 보면 값 수가 고정되어 있지 않음을 알 수 있으므로 (N1, N2 ...) 최대 6 단계로 결정할 수 없습니다. 그것은 더 많거나 적을 수 있습니다 ... –

관련 문제