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


Fastest way to split delimited string in SQL Server


Fastest way to split delimited string in SQL Server

Author
Message
ksatpute123
ksatpute123
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1718 Visits: 383
Comments posted to this topic are about the item Fastest way to split delimited string in SQL Server
Mike Sofen-356112
Mike Sofen-356112
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 59
I would disagree with your contention that this is the fastest way to split a delimited string. You've encapsulated the logic in a UDF, which itself must be called row-by-row, but even worse, it must instantiate the XML "service" each time the UDF is called. In my experience, XML conversions have a higher cost than normal string processing, especially in set-based scenarios. If a delimited string had hundreds or thousands of elements, then perhaps an XML solution might be faster than a CTE-based chopper, but that's a rare situation, and wouldn't follow any normal design pattern.
john.kelly 36005
john.kelly 36005
SSC-Enthusiastic
SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)

Group: General Forum Members
Points: 105 Visits: 50
Just curious how it would handle the test

a<|b<|c>

If | was the separator? Will it fall over on the < characters in the strings?
phegel
phegel
Valued Member
Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)

Group: General Forum Members
Points: 61 Visits: 58
I did a test using a string parser, which returned 512 records, the XML parser took cosistantly 2 ms and my string parser bounced between 0 and 1 ms.

However, I'd like to note that the XML function is yet another take on creating tables from delimited strings and its always good to look at possible faster ways of doing this process.

/*
SET STATISTICS IO ON;
SET STATISTICS TIME ON;
SET NOCOUNT ON;

SELECT [Item] FROM [dbo].[fnCreateTableFromString]
('1||2||3||4||5||6||7||8||9||10||11||12||13||14||15||16||17||18||19||20','||')
-- WHERE Item IN (1,2,3,4,5,6,7,8,9,20)
SELECT [Item] FROM [dbo].[fnCreateTableFromString]
('1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20',';')
SELECT [Item] FROM [dbo].[fnCreateTableFromString]
('1[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20[][]2[][]3[][]4[][]5[][]6[][]7[][]8[][]9[][]10[][]11[][]12[][]13[][]14[][]15[][]16[][]17[][]18[][]19[][]20','[][]')

SET STATISTICS IO OFF;
SET STATISTICS TIME OFF;
SET NOCOUNT OFF;
*/


/*---------------------------------------------------------------------------------------------------------------
Reason : Parses out the String and returns the Items in a table
---------------------------------------------------------------------------------------------------------------*/

CREATE FUNCTION [dbo].[fnCreateTableFromString] (@item_list AS VARCHAR(MAX), @sep NVARCHAR(MAX))
RETURNS @Items TABLE(Item VARCHAR(200))
AS
BEGIN

DECLARE @separator AS CHAR(1)
,@position AS INT
,@item AS VARCHAR(200)
,@OrderNo AS INT



set @separator = ','

SET @item_list = REPLACE(@item_list,@sep,@separator) + @separator

SELECT @position = PATINDEX('%'+@separator+'%', @item_list)

WHILE @position <> 0
BEGIN
SET @Item = LEFT(@item_list, @position-1)
SET @item_list = STUFF(@item_list, 1, @position , '')
SET @position = PATINDEX('%'+ @separator+'%', @item_list)
INSERT @Items( Item)
VALUES( @Item )
END

RETURN
END

GO


wolfkillj
wolfkillj
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2630 Visits: 2582
Check out Jeff Moden's article "Tally OH! An Improved SQL 8K 'CSV Splitter' Function", which explains a method of splitting delimited strings that outperforms the XML method for strings up to 8000 ASCII characters or 4000 Unicode characters.

Jason Wolfkill
Blog: SQLSouth
Twitter: @SQLSouth
gward 98556
gward 98556
SSC-Enthusiastic
SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)

Group: General Forum Members
Points: 120 Visits: 475
Special XML characters in the delimited string can easily make it fail, even a single & or < will prevent this function working. You would probably have to put something in the function to escape the special characters before storing them as XML, then unescape them after they have been selected from the XML.
Sooner or later a user will put a special character in a field, they always do! Hehe
ksatpute123
ksatpute123
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1718 Visits: 383
Thank you for the feedback guys. A lot of valid points raised and I will certainly look into these.
I posted this based on the tests I did on my end but it seems I failed to cover proper ground.
I am in process of working out a even better solution and this time with proper test cases and statistics to validate the results.
I am kind of obsessed with getting rid of R-BAR so the solution will be on these lines only.

Post more observations and suggestions, share if you have a better solution, let us learn together, grow together.
:-D
peter.barnet
peter.barnet
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 10
I would have thought that a SQL CLR function would provide the ideal solution here?

.NET is the right language to process complex types in a RBAR/loop manner; over TSQL's set based.

It would also be faster I would suspect, though measurements aren't to hand.
jose.pla
jose.pla
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 40
I copied the following from a Blog
http://sqlperformance.com/2012/07/t-sql-queries/split-strings
Posted by Aaron Bertrand on July 26, 2012


The most recommended is CLR, which is to create a dll in C# and call it as a function from SQL, according to the tests this is the best for the longer strings.

Still I did not implemented that, seemed that I would not reach that string size, so went for one that is called Modern
It uses a Tally Table from Itzik Ben-Gan which is explained here http://phelabaum.com/archive/2010/03/tally-table-cte/.
This tally table was used in an SQL ServerCentral article

"Tally OH! An Improved SQL 8K “CSV Splitter” Function
By Jeff Moden, 2012/12/28 (first published: 2011/05/02)
"

http://www.sqlservercentral.com/articles/Tally+Table/72993/
But Aaron modified Moden's function a little bit more.



I use this Moden's function, and until now I do trust it works for what I need

CREATE FUNCTION [dbo].[SplitStrings_Moden]
(
@List NVARCHAR(MAX),
@Delimiter NVARCHAR(MAX)
)
RETURNS TABLE
WITH SCHEMABINDING AS
RETURN
WITH E1(N) AS ( SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1),
E2(N) AS (SELECT 1 FROM E1 a, E1 b),
E4(N) AS (SELECT 1 FROM E2 a, E2 b),
E42(N) AS (SELECT 1 FROM E4 a, E2 b),
cteTally(N) AS (SELECT 0 UNION ALL SELECT TOP (DATALENGTH(ISNULL(@List,1)))
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E42),
cteStart(N1) AS (SELECT t.N+1 FROM cteTally t
WHERE (SUBSTRING(@List,t.N,1) = @Delimiter OR t.N = 0))
SELECT Item = SUBSTRING(@List, s.N1, ISNULL(NULLIF(CHARINDEX(@Delimiter,@List,s.N1),0)-s.N1,8000))
FROM cteStart s;

GO



Satisfied
Solomon Rutzky
Solomon Rutzky
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3272 Visits: 3029
peter.barnet (3/17/2015)
I would have thought that a SQL CLR function would provide the ideal solution here?


It does :-)


.NET is the right language to process complex types in a RBAR/loop manner; over TSQL's set based.


It is :-)

It would also be faster I would suspect, though measurements aren't to hand.


Again, it is :-). Several people have published comparisons, including two blogs that were mentioned in the prior post (one by Jeff Moden and another by Aaron Bertrand). Both showing that except for possibly small strings, SQLCLR is far better.

And for anyone who doesn't want to bother with creating the assembly, etc., you can get it already done, and for free, via the SQL# library (which I am the author of).

It is surprising that anyone even wastes their time doing this in T-SQL anymore. Unless, of course, those who are still stuck on SQL Server 2000 (or earlier). Even Azure SQL Database now supports SQLCLR for Assemblies marked as SAFE.

Take care,
Solomon..

SQL# - http://www.SQLsharp.com/
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