views:

32

answers:

1

I have a one row table returned from a query that looks something like this

[Date1] [Date2] [Date3] [Date4] [Date5] [Date6]

and I want all the Dates to stack up like this

[Date1]
[Date2]
[Date3]
[Date4]
[Date5]
[Date6]

How would I go about doing this without a bunch of separate queries and union statements? I have tried playing around with the PIVOT function but am confused since there is nothing to aggregate the row on.

+2  A: 

Try using UNPIVOT, like this:

SELECT Dates
FROM 
    (SELECT * from yourtable) p
UNPIVOT
    (Dates FOR Seq IN 
        ([Date1], [Date2], [Date3], [Date4], [Date5], [Date6])
) AS unpvt
Mark Bannister
This works great thanks. What is the Seq keyword?
Pieces
@Pieces, Seq is not a keyword, it's a column alias. If you change the SELECT Dates... to be SELECT Seq, Dates... you will see the unpivoted column names as values in the Seq column. (I used it as an abbreviation of "Sequence".)
Mark Bannister