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


To split comma delimiterd string


To split comma delimiterd string

Author
Message
ezhil-649633
ezhil-649633
SSC Veteran
SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)

Group: General Forum Members
Points: 233 Visits: 236
Hi,
Can u suggest be better way(based on performance) to split by comma delited string.

Input:
String - 1,'Joy'~2,'Jack'~3,'Rozy'

Expected Result:
As Table

ID Name
1 Joy
2 Jack
3 Rozy
sidhartha.pooboni
sidhartha.pooboni
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 14
Its always better to write a Table Valued Function that would return a table in the desired format.
RBarryYoung
RBarryYoung
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33852 Visits: 9518
Please see this article by some really smart guy who hangs out here.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
arun.sas
arun.sas
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2967 Visits: 3493
ezhil (5/3/2009)
Hi,
Can u suggest be better way(based on performance) to split by comma delited string.

Input:
String - 1,'Joy'~2,'Jack'~3,'Rozy'

Expected Result:
As Table

ID Name
1 Joy
2 Jack
3 Rozy


Hi,

try this statement

create table #temp
(
slno int identity(1,1),
name1 varchar(100)
)


declare @ABC varchar(1000)/*Alwayes should be in max value*/

select @ABC = '1,joy~2,jack~2,rozy~3,X~4,YYY~5'
select @ABC = 'select ''' + replace (@ABC,',',''' union select ''')+''''

insert into #temp (name1)
exec (@ABC)

select * from #temp
RESULT
slno name1
1 1
2 jack~2
3 joy~2
4 rozy~3
5 X~4
6 YYY~5

for removing the '~'

update #temp
set name1 = left(name1,charindex('~',name1,0)-1) from #temp
where name1 like '%~%'

select * from #temp
slno name1
1 1
2 jack
3 joy
4 rozy
5 X
6 YYY

ARUN SAS
Atif-ullah Sheikh
Atif-ullah Sheikh
SSCertifiable
SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)

Group: General Forum Members
Points: 5022 Visits: 5204
Try this Function

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- Select * From [dbo].[fnSplit] ('A,b' , ',')
ALTER FUNCTION [dbo].[fnSplit]
(@pString varchar(5000),@pSplitChar char(1))
returns @tblTemp table (tid int,value varchar(1000))
as
begin

declare @vStartPosition int

declare @vSplitPosition int

declare @vSplitValue varchar(1000)

declare @vCounter int

set @vCounter=1



select @vStartPosition = 1,@vSplitPosition=0

set @vSplitPosition = charindex( @pSplitChar , @pString , @vStartPosition)

if (@vSplitPosition=0 and len(@pString) != 0)

begin

INSERT INTO @tblTemp

(

tid ,

value

)

VALUES

(

1 ,

@pString

)

return --------------------------------------------------------------->>

end

set @pString=@pString+@pSplitChar

while (@vSplitPosition > 0 )

begin

set @vSplitValue = substring( @pString , @vStartPosition , @vSplitPosition - @vStartPosition )

set @vSplitValue = ltrim(rtrim(@vSplitValue))

INSERT INTO @tblTemp

(

tid ,

value

)

VALUES

(

@vCounter ,

@vSplitValue

)

set @vCounter=@vCounter+1

set @vStartPosition = @vSplitPosition + 1

set @vSplitPosition = charindex( @pSplitChar , @pString , @vStartPosition )

end

return
end



For Performance, this function can be useful and handy to be used with parameters with a limited number of List of values. Never Store such value in the database to avoid bottlenecks of string parsing.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sometimes, winning is not an issue but trying.

You can check my BLOG here


Atif-ullah Sheikh
Atif-ullah Sheikh
SSCertifiable
SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)

Group: General Forum Members
Points: 5022 Visits: 5204
Sorry... Disn't told you the usage in your scenario,

here it is...


Declare @v1 varchar(20)
Declare @v2 varchar(1000)

Set @v1 = '1,Joy~2,Jack~3,Rozy'

Select Left([value],charindex(',',[value])-1) as ID,
right([value],len([value]) - charindex(',',[value])) as val
from dbo.fnSplit(@v1,'~')



----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sometimes, winning is not an issue but trying.

You can check my BLOG here


rafidheenm
rafidheenm
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 49
Hi ezhil,

please check this article ..

This article has two types of split.

1. XML split
2. SQL Function (Basic SQL string split for SQL 2000 or later)

http://www.sqlservercentral.com/articles/XML/66932/

With regards,
Rafidheen.M
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)

Group: General Forum Members
Points: 208117 Visits: 41973
rafidheenm (12/22/2009)
Hi ezhil,

please check this article ..

This article has two types of split.

1. XML split
2. SQL Function (Basic SQL string split for SQL 2000 or later)

http://www.sqlservercentral.com/articles/XML/66932/

With regards,
Rafidheen.M


Just saw this so sorry for the really late post on it. Splitting strings using XML is comparatively dog slow and shouldn't be used. Instead, use either a Tally/Numbers table splitter or a cteTally splitter.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)

Group: General Forum Members
Points: 208117 Visits: 41973
RBarryYoung (5/4/2009)
Please see this article by some really smart guy who hangs out here.


Heh... I know it's been over a year and I'm sorry I missed this. Thanks for the nice compliment, Barry.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
RBarryYoung
RBarryYoung
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33852 Visits: 9518
Jeff Moden (7/1/2010)
RBarryYoung (5/4/2009)
Please see this article by some really smart guy who hangs out here.


Heh... I know it's been over a year and I'm sorry I missed this. Thanks for the nice compliment, Barry.


Heh, you're welcome, of course Jeff.

I actually dropped a bunch of those around over the years just to see how long it would take you to find them if I didn't use your name :-P (but I did always link your articles or a post :-D ).

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
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