Solved

Returning Columns from a table vertically

Posted on 2008-06-17
2
212 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

710 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