2011-09-23 7 views
0

SQL에서 분리 된 파이프 구분 문자열 값을 개별 열로 어떻게 할 수 있습니까?SQL 2008 - 파이프 구분 문자열 분할

2008 | 1245 | 0 | 1004 | 1224 | 0 | 간단한 선택 스크립트를 사용

FirstValue SecondValue ThirdValue ForthValue FifthValue SixthValue 
2008  1245  0   1004   1224   0 

.

는 시뮬레이션은 무엇 ... 당신이

+0

음을 시도 감사? –

+0

이 문맥에서'sim'이란 무엇입니까? –

+0

문자열을 [split] [1] 할 수 있습니다. [1] : http://stackoverflow.com/questions/314824/t-sql-opposite-to-string-concatenation-how-to-split-string-into-multiple-recor – PiTheNumber

답변

1

CREATE FUNCTION dbo.fnSplit(
    @sInputList VARCHAR(8000) -- List of delimited items 
    , @sDelimiter VARCHAR(8000) = '|' -- delimiter that separates items 
) RETURNS @List TABLE (item VARCHAR(8000)) 

BEGIN 
DECLARE @sItem VARCHAR(8000) 
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0 
BEGIN 
SELECT 
    @sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))), 
    @sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList)))) 

IF LEN(@sItem) > 0 
    INSERT INTO @List SELECT @sItem 
END 

IF LEN(@sInputList) > 0 
INSERT INTO @List SELECT @sInputList -- Put the last item in 
RETURN 
END 
GO 
--Test 
select * from fnSplit('2008|1245|0|1004|1224|0|','|')