Nirali Nirali - 5 months ago 13
SQL Question

Can I combine the results from multiple columns into a single column without UNION? in SQL Server 2012

I want to combine 2 column output into one column.

Example :

select 1 as id, 2 as exid


Current output:

id | exid
1 | 2


but I want output like the following :

id
1
2


If I use
Concat
function then output looks like this :

id
12


and I don't want to this type of output.

Note : I don't want to use
UNION
function.

Answer

Unpivot will help you to achieve what you are trying to do

select exprid [id] from
(select 1 [id],2 [expid]
) s
unpivot (exprid for col in (id,expid)) as t2

Compared to the using union this should be faster because it will only scan the table once instead of twice.