Background I am have a table which contains a varchar with a comma separated string (which has multiple rows similar to this question. Unfortunately I haven't been able to find a way to change a csv varchar into a temp table simply so I am looking into splitting it up myself. (I would also appreciate any answers tackling this, although maybe it should be a separate question?).
Question
I have successfully split the string up and am now producing an output similar to:
-------------------------------------
| Row Index | Column Index | Value |
-------------------------------------
| 0 | 0 | (0,0) |
| 0 | 1 | (1,0) |
| 0 | 2 | (2,0) |
| 1 | 0 | (0,1) |
| 1 | 1 | (1,1) |
| 1 | 2 | (2,1) |
| 2 | 0 | (0,2) |
| 2 | 1 | (1,2) |
| 2 | 2 | (2,2) |
-------------------------------------
I would like to pivot this so that I can insert it into a temp table and the final result would look like:
-------------------------------------
| Column 1 | Column 2 | Column 3 |
-------------------------------------
| (0,0) | (1,0) | (2,0) |
| (0,1) | (1,1) | (2,1) |
| (0,2) | (1,2) | (2,2) |
-------------------------------------
Asides
- The number of columns is known ahead of time but answers which don't depend on this are also welcome.
- I know that I could repeatedly left outer join the first table to get this result but this requires outer joining once for every column (as I have ~9 columns this will be a lot of repetition) and I assume that there is another way.
- Performance is not key but there will be ~2000 rows with 8 columns in the final table.