Solved

Returning Columns from a table vertically

Posted on 2008-06-17
2
187 Views
Last Modified: 2013-11-05
Hi,
  I have a table which I want to return its data with a query in a vertical way in a single column, for example I have this table

  table_name
  | Field_1 |  Field 2 | Field 3  |
  --------------------------------
  | Value1 | Value2  | Value3 |

 And I need to get
   
  Field_1 = Value1
  Field_2 = Value2
  Field_3 = Value3
 

Thxs in advance
0
Comment
Question by:negreteo
2 Comments
 
LVL 22

Expert Comment

by:dportas
ID: 21806652
How will you make sense of the result if there is more than one row? Is the order significant?

SELECT col, val
FROM
 (SELECT 'col1', col1
  FROM tbl
  UNION ALL
  SELECT 'col2', col2
  FROM tbl
  UNION ALL
  SELECT 'col3', col3
  FROM tbl) T(col,val)
/* ORDER BY ??? something unspecified ? */ ;

0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 500 total points
ID: 21809937
hi, similar to @dportas solution above, but, if I read that properly....

you need to do a union from selecting all the different columns one at a time... have taken the value content as being numeric, not exceeding 20 characters - you might need to adjust accordingly...

select new_column
from (
select 'Field_1 = '+convert(varchar(20),isnull(value1,0)) as new_column from mytable
union all
select 'Field_2 = '+convert(varchar(20),isnull(value2,0)) from mytable
union all
select 'Field_3 = '+convert(varchar(20),isnull(value3,0)) from mytable
--etc
) c
order by new_column
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now