How to Convert a Row in Columns

  •  Hi all ,

          Again I am stuck in a very different type of stage.

          Let suppose I have a Table which have only 3 fields i.e. Name, Subject and Marks

          This table will contain only 1 record, i.e.

    Name      Subject       Marks

    Raj          Maths          99

     

    Now I wants to create another temp table which have only 2 fields i.e. ColName and ColValue

    I want to Insert record in this table like

    ColName              ColValue

    Name                  Raj

    Subject                Maths

    Marks                   99

     

    Please help me out

    Raj


    Thanks & Regards,
    Raj

  • Try something like this:

    --turn a wide table into a narrow table

    declare @Table table (col1 int, col2 int, col3 int)

    insert @Table values (1, 2, 4)

    select 'col1' as colname, col1 as colvalue

    from @Table

    union all

    select 'col2' as colname, col2

    from @Table

    union all

    select 'col3' as colname, col3

    from @Table

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

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