Split and concatenate function

  • tomaz.kastrun

    SSCrazy

    Points: 2085

    Comments posted to this topic are about the item Split and concatenate function

    Tomaž Kaštrun | twitter: @tomaz_tsql | blog:  https://tomaztsql.wordpress.com/

  • ui ranejeb

    SSC Enthusiast

    Points: 138

  • Luis Cazares

    SSC Guru

    Points: 183524

    The best splitter for T-SQL is right here[/url] along with several tests. It will outperform any splitter using while loops or xml.

    With it, you can replace your function to this. Note that it has a limit to 4000 unicode chars (8000 for non-unicode) to avoid the use of large data types and improve performance.

    -- Split and concatenate back to string

    CREATE FUNCTION [dbo].[Split_and_concatenate]

    (

    @STR NVARCHAR(4000)

    ,@delimiter CHAR(1)

    )

    RETURNS NVARCHAR(8000)

    AS

    BEGIN

    SELECT STUFF( (SELECT '; ' + CASE Item WHEN '1' THEN '11'

    WHEN '2' THEN '22'

    ELSE '33' END

    FROM dbo.[DelimitedSplitN4K](@str, @delimiter)

    FOR XML PATH('')),1,2, '')

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 3 posts - 1 through 3 (of 3 total)

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