Connecting Tech Pros Worldwide Forums | Help | Site Map

trick for displaying result? SQL Server 2005

Newbie
 
Join Date: Nov 2009
Posts: 5
#1: 3 Weeks Ago
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's Avatar
Expert
 
Join Date: Jan 2008
Location: Sydney
Posts: 790
#2: 3 Weeks Ago

re: trick for displaying result? SQL Server 2005


How many columns?

You could try
Expand|Select|Wrap|Line Numbers
  1. select 'Col1' as [Field],col1 as Data from .....
  2. union all
  3. select 'Col2' as [Field],col2 as Data from .....
  4. union all
  5. select 'Col3' as [Field],col3 as Data from .....
  6. ........
  7. etc
  8. ........
  9.  

May I ask, if you want the data to display that way, why the table wasn't designed that way
nbiswas's Avatar
Member
 
Join Date: May 2009
Location: India
Posts: 34
#3: 3 Weeks Ago

re: trick for displaying result? SQL Server 2005


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
Newbie
 
Join Date: Nov 2009
Posts: 5
#4: 1 Week Ago

re: trick for displaying result? SQL Server 2005


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
Reply