如何将列拆分为多个值

时间:2011-12-28 11:02:48

标签: sql sql-server sql-server-2000

表1

Value (Always 3 Digit)

100x200x300
200x400x500
...
...

我想从值列

创建3列

预期产出

val1 val2 val3

100 200 300
200 400 500
...
...

需要查询帮助

2 个答案:

答案 0 :(得分:3)

这会利用PARSENAME而不是SUBSTRING。

SELECT
   PARSENAME(Value2, 3) AS val1,
   PARSENAME(Value2, 2) AS val2,
   PARSENAME(Value2, 1) AS val3
FROM
    (
    SELECT
        REPLACE(Value, 'x', '.') AS Value2
    FROM
        MyTable
    ) T;

此代码

  • 允许不同的组件值长度
  • 假设x始终是分隔符
  • 假设总是3个组件

答案 1 :(得分:2)

SELECT SUBSTRING([VALUE],1,3) AS val1,
       SUBSTRING([VALUE],5,3) AS val2, 
       SUBSTRING([VALUE],9,3) AS val3
  FROM TABLE1