I have a data row with columns A, B, C, D, E, F…
How could I write a sql select to have the result:
Col1, Col2
A, B
C, D
E, F
...
EDIT:
There will be only one row and dynamic number of columns. And I am not allowed to use information_schema and sys tables. Also would be interesting to know how to do it for entire table (multiple A,B,C,D,E.. rows), not the only row.
If you know the list of columns ahead of time, it should be as simple as unioning each group of two columns together:
If the list of columns is dynamic, you will need Dynamic SQL to process. Here’s a fully-working example using a test table with 11 columns:
Sample Input:
Sample Output:
Live Demonstration: http://sqlfiddle.com/#!3/d0507/1