Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Separate columns to rows

Author  Topic 

daniel50096230
Yak Posting Veteran

99 Posts

Posted - 2013-12-04 : 01:34:48
Hi, I has the following columns. How can I separate those columns into separate rows.

Eg.

ColumnA ColumnB ColumnC
Ice Lemon Tea
Ice Coca Cola


Result

Column
Ice
Lemon
Tea
Ice
Coca
Cola

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-12-04 : 02:02:09
Select ColumnA from table
union all
Select ColumnB from table
union all
Select ColumnC from table


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-04 : 06:16:37
two other alternatives


declare @c table
(
ColumnA varchar(100),
ColumnB varchar(100),
ColumnC varchar(100)
)
insert @c
values ('Ice','Lemon','Tea'),
('Ice','Coca','Cola')

select col
from @c
cross apply (values (ColumnA),(ColumnB),(ColumnC))t1(Col)


select col
from @c
unpivot (col for val in (ColumnA,ColumnB,ColumnC))u


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -