Splitting Comma separated values in columns to multiple rows in Sql Server(将列中的逗号分隔值拆分为 Sql Server 中的多行)
问题描述
我的表有三列.[Col3
] 列之一具有多个值.因此,当我在表上执行选择命令时:
My table has three columns. One of the columns [Col3
] has multiple values. So, when I make a select command on the table :
Select col1, col2, col3 from MyTable
它给了我以下结果:
Col1 Col2 Col3
------------------------------
Row 1 430 A319 N1160 N1336
Row 2 abc efg G3489 M5678 N5643
如果有什么办法可以得到输出:
If there any way to get the output as:
Col1 Col2 Col3
------------------------------
Row 1 430 A319 N1160
Row 2 430 A319 N1336
Row 3 abc efg G3489
Row 4 abc efg M5678
Row 5 abc efg N5643
如果该列有多个值,则该列中的每个值对应显示一个新行,其他列应包含重复数据.
Like if the column has multiple values, then a new row is displayed corresponding to each value in the column and other columns should contain the duplicated data.
我希望我很清楚这个问题.
I hope I am pretty clear with the problem.
推荐答案
SELECT col1,
col2,
Split.a.value('.', 'VARCHAR(100)') col3
FROM (SELECT col1,
col2,
Cast ('<M>' + Replace(col3, ' ', '</M><M>') + '</M>' AS XML) AS Data
FROM [table]) AS A
CROSS APPLY Data.nodes ('/M') AS Split(a)
这篇关于将列中的逗号分隔值拆分为 Sql Server 中的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将列中的逗号分隔值拆分为 Sql Server 中的多行


基础教程推荐
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01