Split string into Columns based on Special Character

  • Hi,

    Can anyone help me get the required result in SQL 2012

    Create table DBInfo (Path varchar (500))

    Insert into DBInfo values('/Data Sources')

    Insert into DBInfo values('/Data Sources/SALES')

    Insert into DBInfo values('/PRODUCTION')

    Insert into DBInfo values('/PRODUCTION/SERVICE')

    Insert into DBInfo values('/PRODUCTION/SERVICE/MAINTENANCE')

    Insert into DBInfo values('/PRODUCTION/SERVICE/LOGISTICS')

    My Expected Output

    Column1,Column2,Column3

    Data SourcesNullNull

    Data SourcesSalesNull

    PRODUCTIONNullNull

    PRODUCTIONSERVICENull

    PRODUCTIONSERVICEMAINTENANCE

    PRODUCTIONSERVICELOGISTICS

  • This uses Jeff Moden's DelimitedSplit8K function posted at http://www.sqlservercentral.com/articles/Tally+Table/72993/. If you aren't familiar with it yet, take the time to acquainted with it. It's well worth the read.

    select column_a = MAX(case when s.itemnumber = 1 then s.item end),

    column_b = MAX(case when s.itemnumber = 2 then s.item end),

    column_c = MAX(case when s.itemnumber = 3 then s.item end),

    column_d = MAX(case when s.itemnumber = 4 then s.item end)

    from DBInfo

    cross apply DelimitedSplit8K(Path, '/') s

    group by DBInfo.Path;

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

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