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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | 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
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

daniel50096230
Yak Posting Veteran

Malaysia
99 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
22864 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
52326 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  
 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.04 seconds. Powered By: Snitz Forums 2000