Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use the <a href="http://technet.microsoft.com/en-us/library/ms177410%28v=sql.105%29.aspx" rel="nofollow"><code>UNPIVOT</code></a> table operator:</p> <pre><code>SELECT col AS RowValues FROM table1 AS t UNPIVOT ( col FOR value IN([col1], [col2], [col3], [col4], [col5]) ) AS u; </code></pre> <ul> <li><a href="http://sqlfiddle.com/#!3/74c9f/3" rel="nofollow">SQL Fiddle Demo</a></li> </ul> <p>This will give you:</p> <pre><code>| ROWVALUES | |-----------| | hi | | this | | is | | a | | test | </code></pre> <hr> <h2>Update:</h2> <p>In case you don't know the names of the columns, and you want to do this dynamically, you have to do this using dynamic SQL.</p> <p>But the problem is how to get the columns names?</p> <p>You can get the columns names from the <code>information_schema.columns</code>, then concatenate them in one sql, then replace the columns' names in the <code>UNPIVOT</code> with this string, and execute that statement dynamically like this:</p> <pre><code>DECLARE @cols AS NVARCHAR(MAX); DECLARE @query AS NVARCHAR(MAX); select @cols = STUFF((SELECT distinct ',' + QUOTENAME(column_name) FROM information_schema.columns WHERE table_name = 'Table1' FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') , 1, 1, ''); SELECT @query = ' SELECT col AS RowValues FROM table1 AS t UNPIVOT ( val FOR col IN ( ' + @cols + ' ) ) AS u;'; EXECUTE(@query); </code></pre> <ul> <li><a href="http://sqlfiddle.com/#!3/74c9f/7" rel="nofollow"><strong>Updated SQL Fiddle Demo</strong></a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload