拆分具有所需列长sql server的字符串

时间:2014-02-14 09:56:03

标签: sql-server-2012

我正在使用包含行的列

100X200X300
1000X2000X3000

我想将其拆分为

A           B             C
100        200           300
1000       2000          3000

1 个答案:

答案 0 :(得分:0)

你的回答在这里:how to separate string into different columns?

有一个限制,你不需要将sting分成更多的4列

示例:http://sqlfiddle.com/#!3/48354/1

CREATE TABLE sample (MachineName VARCHAR(max))

INSERT INTO sample
VALUES ('100X200X300'),
    ('1000X2000X3000')

SELECT Reverse(ParseName(Replace(Reverse(MachineName), 'X', '.'), 1)) AS [M1],
    Reverse(ParseName(Replace(Reverse(MachineName), 'X', '.'), 2)) AS [M2],
    Reverse(ParseName(Replace(Reverse(MachineName), 'X', '.'), 3)) AS [M3]
FROM (
    SELECT MachineName
    FROM sample
    ) AS [x]