I have a table in a database that has 9 columns containing the same sort of data, these values are allowed to be null. I need to select each of the non-null values into a single column of values that don't care about the identity of the row from which they originated.
So, for a table that looks like this:
+---------+------+--------+------+
| Id | I1 | I2 | I3 |
+---------+------+--------+------+
| 1 | x1 | x2 | x7 |
| 2 | x3 | null | x8 |
| 3 | null | null | null|
| 4 | x4 | x5 | null|
| 5 | null | x6 | x9 |
+---------+------+--------+------+
I wish to select each of the values prefixed with x into a single column. My resultant data should look like the following table. The order needs to be preserved, so the first column value from the first row should be at the top and the last column value from the last row at the bottom:
+-------+
| value |
+-------+
| x1 |
| x2 |
| x7 |
| x3 |
| x8 |
| x4 |
| x5 |
| x6 |
| x9 |
+-------+
I am using SQL Server 2008 R2. Is there a better technique for achieving this than selecting the value of each column in turn, from each row, and inserting the non-null values into the results?