4

I would like to split one records columns into multiple rows.

If I have a SQL statement like the following:

SELECT 1,2,3

Result:

1 | 2 | 3

How do I convert that to the following result?

1
2
3

I am currently using MS SQL 2008.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
ChrisCrous
  • 59
  • 6

2 Answers2

3
SELECT 1
UNION
SELECT 2
UNION
SELECT 3
aF.
  • 64,980
  • 43
  • 135
  • 198
  • Is there no way to do it without modifying to original SELECT statement? ie SELECT converttorow(SELECT 1,2,3) – ChrisCrous May 09 '12 at 11:00
  • 1
    @ChrisCrous: you can create your function or storedprocedure where you just pass these comma separated values and there you can put your t-sql logic to generate such output.. – Niranjan Singh May 09 '12 at 11:27
  • 1
    Check [Convert Comma Separated Values into Rows](http://www.sqlservercentral.com/Forums/Topic1043869-149-1.aspx#bm1043876) and with oracle [Convert comma separated string in rows](https://forums.oracle.com/forums/thread.jspa?threadID=2320358).. [this](http://www.sqlservercentral.com/Forums/Topic968028-338-1.aspx#bm968044) and [this](http://stackoverflow.com/questions/887628/convert-multiple-rows-into-one-with-comma-as-separator).. [this](http://stackoverflow.com/questions/5493510/turning-a-comma-separated-string-into-individual-rows) – Niranjan Singh May 09 '12 at 11:43
0

To sum up the comments above:

  • If you want to convert columns into rows, you will need to use the T-SQL UNPIVOT clause.

    If you want to split a single column that contains comma separated values, you will need to create a Function (example here)

Nathan Griffiths
  • 12,277
  • 2
  • 34
  • 51