convert comma separated string into rows

  • Hi all,

    I am having a table where i am storing comma separated string in that. I want to split that on particular condition and convert into rows

    DECLARE @csv varchar(50)
    SET @csv = 'comma after this, and anothercommahere,thiedvalue,FourthVale,Fifthvalue'

    in the above case, I need output as. date column i m taking from another table.
    if today is getdate then - comma after this
    if  date  = getdate() + 1 then and anothercommaher
    if date = getdate() + 2 then thiedvalue
    if date = getdate() + 3  then FourthVale
    if date = getdate() + 10  then Fifthvalue

    I always need to consider above condition in separate rows. if condition not satisfied then put as a null . 
    appreciate your help.

    Thanks,
    Abhas.

  • Take a look at Alan's SubStringIndex function.  

    You can also look at Jeff's DelimitedSplit8K

  • Strong vote for following Jeff Moden's approach to splitting strings. It's been tested to scale to which most of us will never be messing with.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • abhas - Tuesday, April 10, 2018 12:29 AM

    Hi all,

    I am having a table where i am storing comma separated string in that. I want to split that on particular condition and convert into rows

    DECLARE @csv varchar(50)
    SET @csv = 'comma after this, and anothercommahere,thiedvalue,FourthVale,Fifthvalue'

    in the above case, I need output as. date column i m taking from another table.
    if today is getdate then - comma after this
    if  date  = getdate() + 1 then and anothercommaher
    if date = getdate() + 2 then thiedvalue
    if date = getdate() + 3  then FourthVale
    if date = getdate() + 10  then Fifthvalue

    I always need to consider above condition in separate rows. if condition not satisfied then put as a null . 
    appreciate your help.

    Thanks,
    Abhas.

    Other have already suggested Jeff Moden's string splitter function, and I agree, but I'm not real clear on exactly what your objective is.  If you can post sample data and the create table statements to go with it, along with the expected results based on that sample data, a completely coded solution may be possible...

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • sgmunson - Tuesday, April 10, 2018 10:27 AM

    abhas - Tuesday, April 10, 2018 12:29 AM

    Hi all,

    I am having a table where i am storing comma separated string in that. I want to split that on particular condition and convert into rows

    DECLARE @csv varchar(50)
    SET @csv = 'comma after this, and anothercommahere,thiedvalue,FourthVale,Fifthvalue'

    in the above case, I need output as. date column i m taking from another table.
    if today is getdate then - comma after this
    if  date  = getdate() + 1 then and anothercommaher
    if date = getdate() + 2 then thiedvalue
    if date = getdate() + 3  then FourthVale
    if date = getdate() + 10  then Fifthvalue

    I always need to consider above condition in separate rows. if condition not satisfied then put as a null . 
    appreciate your help.

    Thanks,
    Abhas.

    Other have already suggested Jeff Moden's string splitter function, and I agree, but I'm not real clear on exactly what your objective is.  If you can post sample data and the create table statements to go with it, along with the expected results based on that sample data, a completely coded solution may be possible...

    +1

    ...

Viewing 5 posts - 1 through 4 (of 4 total)

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