SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Looping Comma separated string


Looping Comma separated string

Author
Message
born2achieve
born2achieve
Right there with Babe
Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)

Group: General Forum Members
Points: 716 Visits: 474
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
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18193 Visits: 6431
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
hemang.patel
hemang.patel
Old Hand
Old Hand (358 reputation)Old Hand (358 reputation)Old Hand (358 reputation)Old Hand (358 reputation)Old Hand (358 reputation)Old Hand (358 reputation)Old Hand (358 reputation)Old Hand (358 reputation)

Group: General Forum Members
Points: 358 Visits: 71
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)
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18193 Visits: 6431
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 covers this approach to string splitting. It is recommended reading.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Sean Pearce
Sean Pearce
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4154 Visits: 3436
INSERT INTO sample
SELECT @id, Item FROM dbo.DelimitedSplit8K(@string, ',');





The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Pulivarthi Sasidhar
Pulivarthi Sasidhar
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2474 Visits: 1026
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
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18193 Visits: 6431
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
SrcName
SrcName
Old Hand
Old Hand (338 reputation)Old Hand (338 reputation)Old Hand (338 reputation)Old Hand (338 reputation)Old Hand (338 reputation)Old Hand (338 reputation)Old Hand (338 reputation)Old Hand (338 reputation)

Group: General Forum Members
Points: 338 Visits: 394
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
born2achieve
born2achieve
Right there with Babe
Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)Right there with Babe (716 reputation)

Group: General Forum Members
Points: 716 Visits: 474
wow,

thanks a lot everyone for your time on this post.

dwain's suggestion will be more simple.
Ed Wagner
Ed Wagner
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50265 Visits: 10844
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.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search