Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I wanted to know if it is possible to select 1 value from 2 diferent Columns in the same table and display them in the same column.

Heres an Example.
Total Free Space     |     Total Space
10                            30
20                            10
40                            20

Show something like:
10 --First Column
30 --Second Column

Thank You.
Best Regards.
Posted
Updated 24-May-12 10:38am
v3

SQL
SELECT F1 
FROM Table1
UNION ALL
SELECT F2 AS F1 
FROM Table2


More at MSDN: UNION[^]

[EDIT]
Analogically, if data comes from the same table:
SQL
SELECT F1 
FROM Table1
UNION ALL
SELECT F2 AS F1 
FROM Table1

[/EDIT]
 
Share this answer
 
v2
Comments
Member 8956437 24-May-12 7:20am    
What if they are in the same table?
Maciej Los 24-May-12 8:37am    
In the first post, you have wrote, that columns comes from different tables. So... an answer is as is.
Analogically... See my answer after changes.
VJ Reddy 24-May-12 8:49am    
Good answer. 5!
Maciej Los 24-May-12 11:48am    
Thank you, VJ ;)
Sandeep Mewara 25-May-12 1:54am    
Good one. 5!
you can use union all for this
e.g.
SQL
select column_Name1 from tbl_1 union all select column_Name2 from tbl_2
 
Share this answer
 
v2
Comments
Member 8956437 24-May-12 7:20am    
What if the Values are in the same table, but in diferent Columns
ujju.1 24-May-12 8:52am    
even then, it will be similar to the first one. i.e.
select column_Name1 a from tbl_1 union all select column_Name2 a from tbl_1
You haven't mentioned the exact logic you're trying to use since the answer may vary depending on the needs. But if you want for example pick a value from some column in your query then you can use CASE[^]

An example
SQL
SELECT CASE
          WHEN column1 = 10 THEN column1
          WHEN column2 = 30 THEN column2
          ELSE column3
       END AS ResultColumnName
FROM YourTable
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900