Fastest way to split delimited string in SQL Server

  • Comments posted to this topic are about the item Fastest way to split delimited string in SQL Server

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

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

  • 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

  • Check out Jeff Moden's article "Tally OH! An Improved SQL 8K 'CSV Splitter' Function"[/url], 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

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

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

    ๐Ÿ˜€

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

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

    [p]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/[/url].

    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/[/url]

    But Aaron modified Moden's function a little bit more.[/p]

    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:

  • 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#[/url] 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#https://SQLsharp.com/ ( SQLCLR library ofover 340 Functions and Procedures)
    Sql Quantum Lifthttps://SqlQuantumLift.com/ ( company )
    Sql Quantum Leaphttps://SqlQuantumLeap.com/ ( blog )
    Info sitesCollations     •     Module Signing     •     SQLCLR

  • It does provide some good insight, thanks.

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply