How to get data in multiple columns into a single column

  • Hi All,

    I need to transfer data in multiple columns into a single column

    for example if the source table is like

    Source table:

    Col1 Col2 Col3

    xxx yyy zzz

    I need to get the data into the destination table as 3 rows

    Destination Table:

    Col1

    xxx

    yyy

    zzz

    please help me out in this...

  • you can try it

    pivot

  • Hi can you explain it in detail how to pivot it

  • You don't need a PIVOT but an UNPIVOT method.

    IF ( OBJECT_ID( 'tempdb..#test' ) IS NOT NULL )

    DROP TABLE #test

    CREATE TABLE #test

    (

    id INT NOT NULL IDENTITY(1,1) PRIMARY KEY CLUSTERED,

    col1 VARCHAR(10) NOT NULL,

    col2 VARCHAR(10) NOT NULL,

    col3 VARCHAR(10) NOT NULL

    )

    INSERT#test( col1, col2, col3 )

    SELECT'aaa', 'bbb', 'ccc'

    UNION ALL

    SELECT'xxx', 'yyy', 'zzz'

    SELECTid, colname, colvalue

    FROM#test t

    UNPIVOT ( colvalue FOR colname IN( [col1], [col2], [col3] ) ) up

    IF ( OBJECT_ID( 'tempdb..#test' ) IS NOT NULL )

    DROP TABLE #test

    --Ramesh


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

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