Solved

Returning Columns from a table vertically

Posted on 2008-06-17
2
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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…

623 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