SQL to split a non delimited string into delimited string

  • I have a text string, say '12345', and I need to comma delimit each character in the string, i.e. '1,2,3,4,5'

    I can do this easily enough with a loop, but I was wondering if there's an easier / more efficient way?

    Thanks

  • you can do it through. stuff if it is a column value. e.g.

    STUFF((

    SELECT ',' + CONVERT(NVARCHAR(1000),pu.UserId)

    FROM dbo.WI_ProjectUsers pu

    WHERE pu.ProjectId = CTE.ProjectId

    ORDER BY pu.UserId

    FOR XML PATH(''),

    TYPE).value('.','varchar(max)'),1,1,''),

    follow the link to know more

    http://www.sqlservercentral.com/articles/comma+separated+list/71700/

  • How about this:

    declare @string VARCHAR(100)

    SET @string = '12345'

    ;WITH

    TENS (N) AS (SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL

    SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL

    SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0),

    THOUSANDS (N) AS (SELECT 1 FROM TENS t1 CROSS JOIN TENS t2 CROSS JOIN TENS t3),

    TALLY (N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) FROM THOUSANDS)

    select STUFF(

    (SELECT ','+SUBSTRING(@string,N,1)

    FROM TALLY T_Outer

    WHERE N <= LEN(@string)

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

  • That's pretty slick! Thanks guys.

  • You're welcome,Mr.Wolf 😉

  • Wolfmeister (2/7/2011)


    That's pretty slick! Thanks guys.

    Just making sure... do you understand how ColdCoffee's method works?

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

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • ColdCoffee (2/7/2011)


    You're welcome,Mr.Wolf 😉

    Nicely done, CC.

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

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • If my understanding is correct then this method uses a tally table in conjunction with the SUBSTRING function to get the starting positions of each character in @string, and prefix each character with a comma. FOR XML PATH then concatenates the values into a single string, and finally the STUFF function then replaces the first comma with ''.

  • Jeff Moden (2/7/2011)


    ColdCoffee (2/7/2011)


    You're welcome,Mr.Wolf 😉

    Nicely done, CC.

    Wow, accolades from the highest quarters 🙂 Thanks , Jeff. 🙂

  • Wolfmeister (2/7/2011)


    If my understanding is correct then this method uses a tally table in conjunction with the SUBSTRING function to get the starting positions of each character in @string, and prefix each character with a comma. FOR XML PATH then concatenates the values into a single string, and finally the STUFF function then replaces the first comma with ''.

    Fantastic Mr.Wolf.. Thats exactly what the code does..

  • Wolfmeister (2/7/2011)


    If my understanding is correct then this method uses a tally table in conjunction with the SUBSTRING function to get the starting positions of each character in @string, and prefix each character with a comma. FOR XML PATH then concatenates the values into a single string, and finally the STUFF function then replaces the first comma with ''.

    Absolutely correct. I just wanted to make sure that someone wasn't going to use a "black box" without understanding what it does because they'll need to support it in the future. Thanks for taking the time to write the feedback. 🙂

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

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • No problem, largely thanks to your article on tally tables which I read just last week 🙂

  • Wolfmeister (2/7/2011)


    No problem, largely thanks to your article on tally tables which I read just last week 🙂

    🙂

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

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • DECLARE @Sample VARCHAR(100) = '12345'

    ;WITH cte(CurrPos, LastPos, String)

    AS (

    SELECT1 AS CurrPos,

    DATALENGTH(@Sample) AS LastPos,

    CAST(@Sample AS VARCHAR(MAX)) AS String

    UNION ALL

    SELECTCurrPos + 1 AS CurrPos,

    LastPos,

    STUFF(String, 2 * CurrPos, 0, ',') AS String

    FROMcte

    WHERECurrPos < LastPos

    )

    SELECTString

    FROMcte

    WHERECurrPos = LastPos


    N 56°04'39.16"
    E 12°55'05.25"

  • SwePeso (2/8/2011)


    DECLARE @Sample VARCHAR(100) = '12345'

    ;WITH cte(CurrPos, LastPos, String)

    AS (

    SELECT1 AS CurrPos,

    DATALENGTH(@Sample) AS LastPos,

    CAST(@Sample AS VARCHAR(MAX)) AS String

    UNION ALL

    SELECTCurrPos + 1 AS CurrPos,

    LastPos,

    STUFF(String, 2 * CurrPos, 0, ',') AS String

    FROMcte

    WHERECurrPos < LastPos

    )

    SELECTString

    FROMcte

    WHERECurrPos = LastPos

    Thanks, SwePeso, that's pretty neat as well:-)

Viewing 15 posts - 1 through 15 (of 25 total)

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