trick for displaying result? SQL Server 2005

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GinaMarano
    New Member
    • Nov 2009
    • 5

    trick for displaying result? SQL Server 2005

    I run many queries per day. The results are usually 1 row with many, many columns. Is there a trick/free tool to display this row as 1 column instead?

    example:

    col 1 col 2 col 3 col 4 ....
    data1 data2 data2 data4....

    to

    col1: data1
    col2: data2
    col3: data3
    col4: data4

    for adhock queries.

    Thanks

    ~Gina_M~
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    How many columns?

    You could try
    [code=sql]
    select 'Col1' as [Field],col1 as Data from .....
    union all
    select 'Col2' as [Field],col2 as Data from .....
    union all
    select 'Col3' as [Field],col3 as Data from .....
    ........
    etc
    ........
    [/code]


    May I ask, if you want the data to display that way, why the table wasn't designed that way

    Comment

    • nbiswas
      New Member
      • May 2009
      • 149

      #3
      Give a shot with PIVOT if you are using Sql Server 2005+ else for downward version look into this tutorialhttp://jdixon.dotnetdevelopersjourna...0_and_2005.htm

      Comment

      • GinaMarano
        New Member
        • Nov 2009
        • 5

        #4
        Thanks much for the responses.

        I will give the pivot a try. The union example probably works as well but I was looking for something more generic.

        Also, maybe using FOR XML might give me something easily readable.

        SELECT top 1 *
        FROM orders
        FOR XML RAW('orders'), ELEMENTS

        works pretty good. I was just trying to get something in a readable format to quickly send in an email or such.

        Gina_M

        Comment

        Working...