HELP CONVERTING COMMA-DELIMITED COLUMN TO ROWS

  • CREATE TABLE ANCILLARYVALUES(CUSNO VARCHAR(11), STARTDATE DATE, ENDDATE DATE, VALUELIST VARCHAR(14))

    INSERT INTO ANCILLARY VALUES(CUSNO, STARTDATE, ENDDATE VALUELIST) VALUES('66655544401', 01/01/2015, NULL, 'RX0001,RX0002')

    INSERT INTO ANCILLARY VALUES(CUSNO, STARTDATE, ENDDATE VALUELIST) VALUES('66655544402', 01/01/2015, NULL, 'RX0001')

    INSERT INTO ANCILLARY VALUES(CUSNO, STARTDATE, ENDDATE VALUELIST) VALUES('66655544403', 01/01/2015, NULL, 'RX0001,RX0008')

    I need to transform this data into this:

    CUSNO STARTDATE ENDDATE VALUE

    66655544401 01/01/2015 NULL RX0001

    66655544401 01/01/2015 NULL RX0002

    66655544402 01/01/2015 NULL RX0001

    66655544403 01/01/2015 NULL RX0001

    66655544403 01/01/2015 NULL RX0008

  • Nilssond (7/7/2015)


    CREATE TABLE ANCILLARYVALUES(CUSNO VARCHAR(11), STARTDATE DATE, ENDDATE DATE, VALUELIST VARCHAR(14))

    INSERT INTO ANCILLARY VALUES(CUSNO, STARTDATE, ENDDATE VALUELIST) VALUES('66655544401', 01/01/2015, NULL, 'RX0001,RX0002')

    INSERT INTO ANCILLARY VALUES(CUSNO, STARTDATE, ENDDATE VALUELIST) VALUES('66655544402', 01/01/2015, NULL, 'RX0001')

    INSERT INTO ANCILLARY VALUES(CUSNO, STARTDATE, ENDDATE VALUELIST) VALUES('66655544403', 01/01/2015, NULL, 'RX0001,RX0008')

    I need to transform this data into this:

    CUSNO STARTDATE ENDDATE VALUE

    66655544401 01/01/2015 NULL RX0001

    66655544401 01/01/2015 NULL RX0002

    66655544402 01/01/2015 NULL RX0001

    66655544403 01/01/2015 NULL RX0001

    66655544403 01/01/2015 NULL RX0008

    This:

    select

    av.CUSNO,

    av.STARTDATE,

    av.ENDDATE,

    ca.Item as VALUE

    from

    ANCILLARYVALUES av

    cross apply (select Item from dbo.DelimitedSplit8K(av.VALUELIST,',')ca(Item);

    Code for the function attached.

  • http://www.sqlservercentral.com/articles/Tally+Table/72993/

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

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

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