Thursday 21 February 2013

Remove Delimeter And Get Strings In Separate Columns...



CREATE FUNCTION FN_SeparateDelimeterFromString (@string NVARCHAR(MAX),@separator NCHAR(1))
RETURNS @SeparatedStrings TABLE (string NVARCHAR(MAX))
AS
BEGIN
   DECLARE @position int
   SET @position = 1
   SET @string = @string + @separator
   WHILE charindex(@separator,@string,@position) <> 0
      BEGIN
         INSERT into @SeparatedStrings
         SELECT substring(@string, @position, charindex(@separator,@string,@position) - @position)
         SET @position = charindex(@separator,@string,@position) + 1
      END
     RETURN
END
 
Declare @Strings varchar(max)
Set @Strings='1,2,3,4,5,6,7'
select string from FN_SeparateDelimeterFromString(@Strings,',')

select string from FN_SeparateDelimeterFromString('N,I,T,I,S,H,,K,U,M,A,R',',')

No comments:

Post a Comment