• BrainDonor (8/16/2012)


    The easiest way is probably to use the CSV Splitter function, by Jeff Moden (with some assistance from others) - http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    The splitter function will return 3 rows per input row, not 3 columns as was requested. If you apply the splitter function, you will still have to unpivot the resulting rows into columns. And this is where Suresh B.'s question comes in: how many values are there in the input? If there are always 3 values, then it may be easier to do this with charindex() and substring(). If the number of values can vary you should have a look at the link for cross tab in my signature: using a cross tab you can turn the rows into columns again. But also when it is always 3 values you should still read it.

    Here's a little example of how you can turn the rows back into columns using a cross tab:

    declare @tbaleOne table (

    tbale_id int identity(1,1) not null,

    Column1 varchar(100) not null,

    primary key(tbale_id)

    );

    insert @tbaleOne( Column1)

    select 'AA,BB,CC'

    union all select 'DD,EE,FF'

    union all select 'GG,HH,II';

    set ansi_warnings off;

    select max(case x.pos when 1 then x.val end) as col1,

    max(case x.pos when 4 then x.val end) as col2,

    max(case x.pos when 7 then x.val end) as col3

    from @tbaleOne i

    cross apply (

    select 1 as pos, substring(i.Column1, 1, 2) as val

    union all

    select 4 as pos, substring(i.Column1, 4, 2) as val

    union all

    select 7 as pos, substring(i.Column1, 7, 2) as val

    ) x

    group by i.tbale_id;

    set ansi_warnings on;



    Posting Data Etiquette - Jeff Moden[/url]
    Posting Performance Based Questions - Gail Shaw[/url]
    Hidden RBAR - Jeff Moden[/url]
    Cross Tabs and Pivots - Jeff Moden[/url]
    Catch-all queries - Gail Shaw[/url]


    If you don't have time to do it right, when will you have time to do it over?