Search code examples
sql-server-2008t-sqlsql-server-2005unpivot

Unpivot columns to generate rows


I have a query like this:

SELECT '35111212', '11245452', '42215512'...... and more values.

This results in:

(No column name)    (No column name)    (No column name)
--------           --------            --------
35111212           11245452            42215512

And I need to transform it to:

(No column name)
--------
35111212
11245452
42215512

Is that possible without using a "union"? I have a large amount of values in the select.


Solution

  • You can do this, if your columns are named:

    select val from  
    ( 
       select col1 = '1', col2 = '2', col3 = '3'
    ) a
    unpivot 
    (
       var for col in (col1, col2, col3)
    ) as unpvt