Sunday, August 14, 2022

Get a Table from Comma separated value SQL

 Create a function :

CREATE FUNCTION split_string

(

    @in_string VARCHAR(MAX),

    @delimeter VARCHAR(1)

)

RETURNS @list TABLE(tuple VARCHAR(100))

AS

BEGIN

        WHILE LEN(@in_string) > 0

        BEGIN

            INSERT INTO @list(tuple)

            SELECT left(@in_string, charindex(@delimiter, @in_string+',') -1) as tuple

    

            SET @in_string = stuff(@in_string, 1, charindex(@delimiter, @in_string + @delimiter), '')

        end

    RETURN 

END


Calling a function: SELECT * FROM split_string('1001,1002,1003,1004', ',')






No comments:

Post a Comment

Screen Record

 Windows Screen Record WindowsKey+ Alt + R Recording Starts.