SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Separate columns to rows
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

daniel50096230
Yak Posting Veteran

Malaysia
97 Posts

Posted - 12/04/2013 :  01:34:48  Show Profile  Reply with Quote
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

India
22761 Posts

Posted - 12/04/2013 :  02:02:09  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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

India
52317 Posts

Posted - 12/04/2013 :  06:16:37  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000