Looping Comma separated string

  • Hi,

    I have a stored proc which has two input parameters.

    @id bigint, @string nvarchar(max)

    @string is comma delimited ex : "100,200,300,400,500".

    I wanted to split this comma delimited string and insert into table "Sample" with same id.

    sample input my proc :

    1, "100,200,300,400,500"

    desired output :

    Table Name : sample:

    1 100

    1 200

    1 300

    1 400

    1 500

    Jeff has give nice way to split the string with the delimiter.

    select * from dbo.DelimitedSplit8K('100,200,300,400,500', ',')

    Is there any way to avoid looping(while loop) and insert into "sample" table

    or any suggestion how to proceed this more faster way

  • Like this?

    INSERT INTO SampleTable

    SELECT 1, Item

    FROM dbo.DelimitedSplit8K('100,200,300,400,500', ',')


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Hi,

    Try these

    Declare @id bigint = 1

    Declare @string as varchar(100) ='100,200,300,400,500'

    DECLARE @testXML XML

    SET @testXML = N'<test>' + REPLACE(@string,',','</test><test>') + '</test>'

    SELECT @id [id],node.value('.','varchar(100)') as [No]

    FROM @testXML.nodes('/test') as records(node)

  • hemang.patel (10/21/2013)


    Hi,

    Try these

    Declare @id bigint = 1

    Declare @string as varchar(100) ='100,200,300,400,500'

    DECLARE @testXML XML

    SET @testXML = N'<test>' + REPLACE(@string,',','</test><test>') + '</test>'

    SELECT @id [id],node.value('.','varchar(100)') as [No]

    FROM @testXML.nodes('/test') as records(node)

    How does that insert into a sample table like the OP asked?

    The discussion thread of the article that presents DelimitedSplit8K[/url] covers this approach to string splitting. It is recommended reading.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • INSERT INTO sample

    SELECT @id, Item FROM dbo.DelimitedSplit8K(@string, ',');

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

  • Hi,

    Use the following Function....

    CREATE FUNCTION fn_getrowsfrmCSVstring(@str varchar(max))

    returns @rtn table (id int identity(1,1),value varchar(2000))

    As

    Begin

    Declare

    @mstr varchar(max),@i INT,@len INT

    SELECT @mstr=@str,@len=LEN(@str),@i=1

    WHILE @i<@len

    BEGIN

    IF CHARINDEX(',',@str)>0

    SELECT @STR=SUBSTRING(@str,1,CHARINDEX(',',@str)-1)

    ELSE

    Select @STR=SUBSTRING(@str,1,LEN(@str))

    INSERT INTO @rtn SELECT @STR

    SET @i=@i+LEN(@str)+1

    SET @STR=SUBSTRING(@mstr,@i,@len)

    END

    Return

    END

  • Pulivarthi Sasidhar (10/22/2013)


    Hi,

    Use the following Function....

    CREATE FUNCTION fn_getrowsfrmCSVstring(@str varchar(max))

    returns @rtn table (id int identity(1,1),value varchar(2000))

    As

    Begin

    Declare

    @mstr varchar(max),@i INT,@len INT

    SELECT @mstr=@str,@len=LEN(@str),@i=1

    WHILE @i<@len

    BEGIN

    IF CHARINDEX(',',@str)>0

    SELECT @STR=SUBSTRING(@str,1,CHARINDEX(',',@str)-1)

    ELSE

    Select @STR=SUBSTRING(@str,1,LEN(@str))

    INSERT INTO @rtn SELECT @STR

    SET @i=@i+LEN(@str)+1

    SET @STR=SUBSTRING(@mstr,@i,@len)

    END

    Return

    END

    Just guessing mind you but I believe the DelimitedSplit8K FUNCTION will be a tad faster than this approach. Suggest you read the linked article as well.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • declare @input varchar(50)='100,200,300,400,500'

    select '1', q2.value from

    ( SELECT cast('<x>'+replace(@input,',','</x><x>')+'</x>' as xml) as txml ) q1 CROSS APPLY

    ( SELECT x.value('.','varchar(100)') as value FROM txml.nodes('x') as f(x) )q2

  • wow,

    thanks a lot everyone for your time on this post.

    dwain's suggestion will be more simple.

  • born2achieve (10/22/2013)


    wow,

    thanks a lot everyone for your time on this post.

    dwain's suggestion will be more simple.

    It'll be significantly faster, too. If you haven't read the article on the delimited string splitter, I'd highly suggest you do so. This is a serious function to have in your toolbox.

  • SrcName (10/22/2013)


    declare @input varchar(50)='100,200,300,400,500'

    select '1', q2.value from

    ( SELECT cast('<x>'+replace(@input,',','</x><x>')+'</x>' as xml) as txml ) q1 CROSS APPLY

    ( SELECT x.value('.','varchar(100)') as value FROM txml.nodes('x') as f(x) )q2

    You too should have a read of that article. In case you missed it, you can find it in my signature too. The article about splitting strings. It is a serious mind opening read.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Hello Ed Wagner,

    I totally agree that "DelimitedSplit8K" function is much faster. thanks for your time.

  • born2achieve (10/22/2013)


    Hello Ed Wagner,

    I totally agree that "DelimitedSplit8K" function is much faster. thanks for your time.

    No problem - glad I could help. I cannot, however, take any credit for coming up with DelimitedSplit8K. That credit goes to Jeff Moden.

  • Yes i agree that credit goes to Jeff.

    by the way, i am good with insert statement. Any suggestion with update statement? how can i update the sample table with the out come of funtion returns table? any suggestions please

  • can any one please suggest me

Viewing 15 posts - 1 through 15 (of 16 total)

You must be logged in to reply to this topic. Login to reply