Skip to content Skip to sidebar Skip to footer

How To Create A Sql Function Which Splits Comma Separated Value?

I want to create a function in SQL Server which takes a comma separated string a parameter, splits it and returns one value at a time. The basic idea here is to call that function

Solution 1:

Like I mention, you'll have to use a CURSOR to do this, however, the fact you want to do it this way infers a (large) design flaw:

DECLARE @value varchar(8000)

DECLARE Delimited_Values CURSOR FAST_FORWARD
FOR
SELECT [value]
FROM STRING_SPLIT('a,b,c,d,e',',')

OPEN Delimited_Values;

FETCH NEXT FROM Delimited_Values
INTO @value;

WHILE @@FETCH_STATUS = 0 BEGIN

    SELECT @value; --Do your stuff here

    FETCH NEXT FROM Delimited_Values
    INTO @value;

END;

CLOSE Delimited_Values;
DEALLOCATE Delimited_Values;

Post a Comment for "How To Create A Sql Function Which Splits Comma Separated Value?"