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


Split strings alternative to XML


Split strings alternative to XML

Author
Message
MackF
MackF
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 78
Hi guys,
For some performance considerations, we would like to try alternative to the code below.
Anyone could help on how to get this job done using CTE, temp table or I don not know...
Actually, I am not a dev guy at all ::-)
Thanks,
Mack

CREATE FUNCTION [dbo].[fn_Split](@data NVARCHAR(MAX), @delimiter NVARCHAR(5))
RETURNS @t TABLE (Idx int identity (1,1), data NVARCHAR(max))
AS
BEGIN

DECLARE @textXML XML;
SELECT @textXML = CAST('<d>' + REPLACE(@data, @delimiter, '</d><d>') + '</d>' AS XML);

INSERT INTO @t(data)
SELECT T.split.value('.', 'nvarchar(max)') AS data
FROM @textXML.nodes('/d') T(split)

RETURN



Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41486 Visits: 38567
Check out the resources with this article: http://www.sqlservercentral.com/articles/Tally+Table/72993/. You should find a CLR splitter that will meet your needs.

Be sure to read the article and the discussion with it.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
MackF
MackF
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 78
Thanks Lynn,
...I have gone through the article, too much complicated to me and definitely not my stuff...
The thing is I know the function above is killing perf, but I do not know how to write something else despite my readings.
The goal is to submit some results to the dev team and show how crappy then can produce their coding when not being concerned by global performance...
If anyone can "translate" this little function, ideally not in CLR, that would be helpful Smile



Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41486 Visits: 38567
Does your function have to split a string declared as nvarchar(max)?

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
MackF
MackF
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 78
Not sure this is absolutely necessary, maybe varchar(some values) could fit, but as of now, I do not know "where" they could use this function to split "what" amout of data...
It could some comments in a text box as it is commercial app...
nvarchar(max) type should be tested at least to compare performance.



Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41486 Visits: 38567
The two T-SQL functions, DelimitedSplit8K and DelimitedSplitN4K, that are attached to the article I referred you to are optimized to split varchar(8000) and nvarchar(4000) strings.

I would read the article and discussion again, as use these functions on appropriate sized strings to see how they work.

For string longer than varchar(8000) and nvarchar(4000) you will probably want to go to a CLR splitter as it will out perform a T-SQL based splitter.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
MackF
MackF
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 78
I am trying...but I do not succeed in adapting the 8k function.
Don't know how to "integrate" the <d>, <d/>, etc stuff as they exist in the initial function...
I really suck



MackF
MackF
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 78
I have got it...

create function Split_fnOK
(
@data varchar(8000),
@deli_char varchar(3)
)
returns @list table
(
Idx int,
data varchar(8000)
)
as
begin

declare @from_loc int
declare @to_loc int

if charindex(@deli_char,@data,0) <= 0
begin
insert into @list(Idx, data) values (1, @data)
return
end

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

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

while @to_loc is not null
begin

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

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


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

With that set up, I have got much better stats time/cpu and I/O. Good.
Cheers



Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27366 Visits: 17557
MackF (3/27/2013)
I have got it...

...

With that set up, I have got much better stats time/cpu and I/O. Good.
Cheers


This is going to be a LOT slower than the methods Lynn suggested. Actually orders of magnitude slower. I know you have been pointed to it before but check out the article in my signature about splitting strings. Then compare your looping function to DelimitedSplit8K and see which one is faster. :-P

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)

Group: General Forum Members
Points: 91194 Visits: 41151
MackF (3/27/2013)
I have got it...

create function Split_fnOK
(
@data varchar(8000),
@deli_char varchar(3)
)
returns @list table
(
Idx int,
data varchar(8000)
)
as
begin

declare @from_loc int
declare @to_loc int

if charindex(@deli_char,@data,0) <= 0
begin
insert into @list(Idx, data) values (1, @data)
return
end

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

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

while @to_loc is not null
begin

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

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


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

With that set up, I have got much better stats time/cpu and I/O. Good.
Cheers


Show us 2 lines of sample data to split so we can show you how to blow the doors off of everything but a CLR and come pretty close to that, to boot.

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