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.

2
  • 3
    Is this really what you want to do? If not you should post something more realistic. Commented May 9, 2012 at 10:11
  • You might want to take a look at UNPIVOT. Commented May 9, 2012 at 10:17

2 Answers 2

3
SELECT 1
UNION
SELECT 2
UNION
SELECT 3
Sign up to request clarification or add additional context in comments.

3 Comments

Is there no way to do it without modifying to original SELECT statement? ie SELECT converttorow(SELECT 1,2,3)
@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..
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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.