Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Looping Comma separated string Expand / Collapse
Author
Message
Posted Monday, October 21, 2013 7:30 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, April 9, 2014 11:31 AM
Points: 175, Visits: 351
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
Post #1506945
Posted Monday, October 21, 2013 9:07 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 10:53 PM
Points: 3,421, Visits: 5,359
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!
Post #1506953
Posted Monday, October 21, 2013 9:50 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, January 3, 2014 12:02 AM
Points: 356, 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)
Post #1506958
Posted Monday, October 21, 2013 10:03 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 10:53 PM
Points: 3,421, Visits: 5,359
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!
Post #1506961
Posted Tuesday, October 22, 2013 3:40 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 8:05 AM
Points: 943, Visits: 2,951
INSERT INTO sample
SELECT @id, Item FROM dbo.DelimitedSplit8K(@string, ',');





The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Post #1507034
Posted Tuesday, October 22, 2013 3:53 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 3:43 AM
Points: 1,063, Visits: 713
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
Post #1507036
Posted Tuesday, October 22, 2013 4:02 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 10:53 PM
Points: 3,421, Visits: 5,359
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!
Post #1507040
Posted Tuesday, October 22, 2013 4:51 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, October 2, 2014 3:14 PM
Points: 108, Visits: 338
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
Post #1507046
Posted Tuesday, October 22, 2013 5:20 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, April 9, 2014 11:31 AM
Points: 175, Visits: 351
wow,

thanks a lot everyone for your time on this post.

dwain's suggestion will be more simple.
Post #1507064
Posted Tuesday, October 22, 2013 5:48 AM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 11:54 AM
Points: 4,246, Visits: 3,686
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
Post #1507078
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse