Convert SQL columns to rows

泄露秘密 提交于 2020-02-03 08:06:25

问题


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.


回答1:


SELECT 1
UNION
SELECT 2
UNION
SELECT 3



回答2:


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)



来源:https://stackoverflow.com/questions/10513820/convert-sql-columns-to-rows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!