0

I have a comma separated value as nvarchar in sql. which i need to convert into table. Please advise.

example of string

'f143bda4-a917-479c-8360-b63943b91d91,f312f49b-203e-4bba-a74e-82ea356ed6d3'

I am using sql server 2005

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Amit
  • 6,839
  • 21
  • 56
  • 90

1 Answers1

1

Are you looking for something like this...

FUNCTION [dbo].[fx_Split]
(
    @text varchar(max),
    @splitChar char(1)
)
RETURNS 
@Result TABLE 
(
    RowIndex int identity(1,1),
    SplitText varchar(max) -- choose your type here...
)
AS
BEGIN

    DECLARE @index int SET @index = 0
    DECLARE @SplitText varchar(max) SET @SplitText = ''
    DECLARE @TempText varchar(max) SET @SplitText = ''

    SET @index = CHARINDEX(@splitChar, @text) 
    SET @TempText = @text

    WHILE(@index > 0)
    BEGIN

        INSERT INTO @Result VALUES (SUBSTRING(@TempText, 1, @index-1))

        SET @TempText = LTRIM(SUBSTRING(@TempText, @index + 1, LEN(@TempText)))

        SET @index = CHARINDEX(@splitChar, @TempText) 

    END 

    INSERT INTO @Result VALUES (@TempText)

    RETURN 
END
Yves M.
  • 3,330
  • 14
  • 12
  • Yes exactly something like this. Also please can you tell me how i can the syntax of how i ca get the returned table into a temporary table variable – Amit Nov 18 '10 at 12:37