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


Split string using XML


Split string using XML

Author
Message
Eralper
Eralper
SSC Eights!
SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)SSC Eights! (970 reputation)

Group: General Forum Members
Points: 970 Visits: 466
Hi Divya,
Is this a new article?

Eralper
SQL Server and T-SQL Tutorials and Articles
Microsoft Certification and Certification Exams
Divya Agrawal
Divya Agrawal
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1054 Visits: 604
yes

--Divya
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)

Group: General Forum Members
Points: 217022 Visits: 41991
Kindred spirit, Goldie. :-)

--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 (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)

Group: General Forum Members
Points: 217022 Visits: 41991
Eralper (7/10/2009)
Actually, I made a simple test to see that the xml function is quicker.


Actually, let's see THAT test. ;-) I already posted mine.

--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
rafidheenm
rafidheenm
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 49
Hi all,
sorry for the late reply...

I agreed with performance issue of SQL function incase of too large string needs to be split.

hence, i have slightly modified the SQL function which makes better performance.

Please try the below code...



CREATE function Split_fn
(
@split_string varchar(8000),
@deli_char varchar(3)
)
returns @list table
(
SeqNo int,
SplitString varchar(8000)
Primary Key (SeqNo)
)
as
begin

declare @from_loc int
declare @to_loc int

if charindex(@deli_char,@split_string,0) <= 0
begin
insert into @list(seqno, SplitString) values (1, @split_string)
return
end

if charindex(@deli_char,@split_string,0) > 0
begin
select @from_loc = 0
select @to_loc = charindex(@deli_char,@split_string,0)
end

if charindex(@deli_char,@split_string,0) <= 0
begin
select @to_loc = null
end

while @to_loc is not null
begin

if substring(@split_string,@from_loc, @to_loc - @from_loc) <> ''
begin
insert into @list(seqno, SplitString)
select isnull(max(seqno),0) + 1, substring(@split_string,@from_loc, @to_loc - @from_loc)
from @list
end

select @from_loc = charindex(@deli_char,@split_string,@from_loc+len(@deli_char)) + len(@deli_char)
select @to_loc = charindex(@deli_char,@split_string,@from_loc)


if @to_loc = 0
begin
if substring(@split_string,@from_loc, (len(@split_string) - @from_loc) + len(@deli_char)) <> ''
begin
insert into @list(seqno, SplitString)
select isnull(max(seqno),0) + 1, substring(@split_string,@from_loc, (len(@split_string) - @from_loc) + len(@deli_char))
from @list
end
select @to_loc = null
end
end
return
end





With regards,
Rafidheen.M
rafidheenm
rafidheenm
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 49
I have defined @split_string, SplitString (column) as varchar(8000). For 2005 or later users it can be varchar(max) (for too large string process)which will not affect the performance..
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)

Group: General Forum Members
Points: 217022 Visits: 41991
I know it's an old post but I thought I'd provide an update just in case anyone is still thinking about using XML for splitting. Please see the following article...
http://www.sqlservercentral.com/articles/Tally+Table/72993/

If there were any doubt before, there isn't now. :-)

--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
sam.walker
sam.walker
SSC-Enthusiastic
SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)

Group: General Forum Members
Points: 147 Visits: 107
Jeff,

I maintain the fastest way of splitting a string is a combined process.

1. Pass the string, delim, to clr function
2. The clr function converts each item in to a fixed char width item, eg 20 chars per item.
3. The returned string is split by an inline function querying a tally table using substring.

I've tried all other methods and they are much slower.

Pure clr is slow because it is slow to pass back so many records.

Pure SQL is slow because it is slow at lookup and constructor functions.

Jeff, if you want the exact code, happy to send it through.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)

Group: General Forum Members
Points: 217022 Visits: 41991
sam.walker (5/9/2011)
Jeff,

I maintain the fastest way of splitting a string is a combined process.

1. Pass the string, delim, to clr function
2. The clr function converts each item in to a fixed char width item, eg 20 chars per item.
3. The returned string is split by an inline function querying a tally table using substring.

I've tried all other methods and they are much slower.

Pure clr is slow because it is slow to pass back so many records.

Pure SQL is slow because it is slow at lookup and constructor functions.

Jeff, if you want the exact code, happy to send it through.


I've not found CLR code to be slow for splitters when they're done properly. Please see the code at the article I posted and test yours against the code that's in there and then post your results here. There's also a standard test data setup for your tests. A simple modification of the code to include your method will allow the automatic running and reporting of your code and the other CLR for 1-10, 10-20, 20-30, 30-40, and 40-50 random length elements across an even wider range of number of elements. Why do I want you to do it? After that article, I'm a bit burned out on testing everyone else's code. :-D

Also, what do you do when you have items that are 21 characters in length?

--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
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