Unpivot

  • I have a table like below:

    Emp

    ====

    id name

    -- -----

    1 x

    now, i want the resuts in the below format:

    col1 col2

    -- ---

    id 1

    name x

    Can someone tell me how should achieve it using UNPIVOT

    thanks


    erajendar

  • Please decide by yourself which option is easier to read....

    DECLARE @tbl TABLE

    (

    id INT, name VARCHAR(30)

    )

    INSERT INTO @tbl SELECT 1 ,'x'

    -- option a

    SELECT 'id' AS col1,CAST(id AS VARCHAR(30)) AS col2 FROM @tbl

    UNION ALL

    SELECT 'name',name FROM @tbl

    -- option b

    SELECT col1,col2

    FROM

    (SELECT CAST(id AS VARCHAR(30)) AS id,name

    FROM @tbl

    ) p

    UNPIVOT

    (col2 FOR col1 IN

    (id,name)

    )AS unpvt



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

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

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