2014-02-10 2 views

답변

0

는 당신이 필요로하는 분할 기능, 그냥 : 난 단지 하나의 열 이름 데이터

내가

DATA 
new 
old,yes,now 
ok,for 
no 

내 필요한 출력과 같은 값을 가지고있다가 이 기능이 작동하는지 확인하십시오.

 CREATE FUNCTION FNC_SPLIT(@MYSTR VARCHAR(500), @DELIMITER CHAR(1)) 
     RETURNS @MYTBL TABLE (idx smallint, value varchar(8000)) 
     AS 
     BEGIN 
     DECLARE @RET VARCHAR(500) 
     DECLARE @INDEX INT 
     DECLARE @COUNTER smallint 

     --Get the first position of delimiter in the main string 
     SET @INDEX = CHARINDEX(@DELIMITER,@MYSTR) 
     SET @COUNTER = 0 

     --Loop if delimiter exists in the main string 
     WHILE @INDEX > 0 
     BEGIN 
      --extract the result substring before the delimiter found 
      SET @RET = SUBSTRING(@MYSTR,1, @INDEX-1) 
      --set mainstring right part after the delimiter found 
      SET @MYSTR = SUBSTRING(@MYSTR,@INDEX+1 , LEN(@MYSTR) - @INDEX) 
      --increase the counter 
      SET @COUNTER = @COUNTER + 1 
      --add the result substring to the table 
      INSERT INTO @MYTBL (idx, value) 
      VALUES (@COUNTER, @RET) 
      --Get the next position of delimiter in the main string 
      SET @INDEX = CHARINDEX(@DELIMITER,@MYSTR) 
     END 

     --if no delimiter is found then simply add the mainstring to the table 
     IF @INDEX = 0 
     BEGIN 
      SET @COUNTER = @COUNTER + 1 
      INSERT INTO @MYTBL (idx, value) 
      VALUES (@COUNTER, @MYSTR) 
     END 
     RETURN 
     END 

     GO 

     declare @table table(dt varchar(100)); 
     insert into @table values 
     ('DATA'), 
     ('new'), 
     ('old,yes,now'), 
     ('ok,for'); 

     select * from @table 

     select value from @table t cross apply dbo.FNC_SPLIT(t.dt,',') 
관련 문제