Ms Sql中的多个拆分

时间:2013-09-18 11:15:11

标签: sql-server sql-server-2008 split delimited-text

我有两个分隔符‘|’竖线和‘,’逗号

的格式字符串
'1,100,12345|2,345,433|3,23423,123|4,33,55'

并且必须插入表格列中,如下所示

+-----+---------+--------+
| seq | invoice | amount |
+-----+---------+--------+
|   1 |     100 |  12345 |
|   2 |     345 |    433 |
|   3 |   23423 |    123 |
|   4 |      33 |     55 |
+-----+---------+--------+

请帮忙 谢谢 Prathibha

1 个答案:

答案 0 :(得分:1)

单程

DECLARE @S VARCHAR(MAX) = '1,100,12345|2,345,433|3,23423,123|4,33,55'

DECLARE @x xml = '<r><c>' + 
                 REPLACE(REPLACE(@S, ',','</c><c>'),'|','</c></r><r><c>') + 
                 '</c></r>'


SELECT x.value('c[1]','int') AS seq,
       x.value('c[2]','int') AS invoice,
       x.value('c[3]','int') AS amount
FROM @x.nodes('/r') x(x)