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


Split string using XML


Split string using XML

Author
Message
Divya Agrawal
Divya Agrawal
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1430 Visits: 604
Comments posted to this topic are about the item Split string using XML

--Divya
jagdish b
jagdish b
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 19
Very useful.
Oded Bet-Av
Oded Bet-Av
SSC-Enthusiastic
SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)SSC-Enthusiastic (144 reputation)

Group: General Forum Members
Points: 144 Visits: 297
what about performance?
Abdul-202005
Abdul-202005
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 Visits: 8
Good. Different approach.
Øystein Fallo
Øystein Fallo
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1591 Visits: 454
Be aware of the possibility of markup characters within your data.
Eg, the following split will fail:
Declare @xml as xml,@str as varchar(100),@delimiter as varchar(10)
SET @str='A<2,B>4,C,D,E'
SET @delimiter =','
SET @xml = cast((''+replace(@str,@delimiter ,'')+'') as xml)


rafidheenm
rafidheenm
SSC-Enthusiastic
SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)

Group: General Forum Members
Points: 114 Visits: 49
Hi all,

Its a new way to split the string using XML. I have written a SQL function to split the string without using XML. Please refer the below code...

CREATE function Split_fn
(
@split_string varchar(max),
@deli_char varchar(3)
)
returns @list table
(
SeqNo int,
SplitString varchar(max)
)
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


go
select * from dbo.split_fn('raja,ravi,prabhu',',')


Rafidheen.M
Carl Federl
Carl Federl
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10452 Visits: 4353
This technique was documented by Erland Sommarskog, SQL Server MVP in 2004. The XML and other methods can be found in the classic article "Arrays and Lists in SQL Server".

For SQL Server 2000, see http://www.sommarskog.se/arrays-in-sql-2000.html and for SQL Server 2005, see http://www.sommarskog.se/arrays-in-sql-2005.html

SQL = Scarcely Qualifies as a Language
Sam Ellis
Sam Ellis
Valued Member
Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)

Group: General Forum Members
Points: 71 Visits: 139
What about using Tally tables to do the same:

http://www.sqlservercentral.com/articles/T-SQL/62867/
Senthilnathan.Karunakaran
Senthilnathan.Karunakaran
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 23
We did performance and scaling test to split the comma separated string value using XML query and SQL function.

The plan was better with XML split than SQL function; however XML query performance degraded when number of concurrent users increase. SQL function did better in scalability test.
Senthilnathan.Karunakaran
Senthilnathan.Karunakaran
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 23
We did performance and scaling test to split the comma separated string value using XML query and SQL function.

The plan was better with XML split than SQL function; however XML query performance degraded when number of concurrent users increase. SQL function did better in scalability test.
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