我想从Chunks中的特定字段获取数据

时间:2017-04-08 07:09:49

标签: sql

我的查询为

SELECT SUBSTR('GENERAL - COMPUTERS - COMPUTER WORKSTATION', 1, (INSTR('GENERAL - COMPUTERS - COMPUTER WORKSTATION', '-')-1)) first, 
substr('GENERAL - COMPUTERS - COMPUTER WORKSTATION', instr('GENERAL - COMPUTERS - COMPUTER WORKSTATION', '-') + 1) second
from mtl_categories_v
where segment1 = '01';

它将结果表示为:

first                 second
GENERAL    COMPUTERS - COMPUTER WORKSTATION

但我希望查询结果为

first                 second                            third
GENERAL              COMPUTERS                 COMPUTER WORKSTATION

请告诉我在破折号之前和之后获取字符的方法。

1 个答案:

答案 0 :(得分:0)

假设您使用的是MySQL:

SELECT TRIM(SUBSTRING_INDEX(col, '-', 1)) AS first,
       TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(col, '-', 2), '-', -1)) AS second,
       TRIM(SUBSTRING_INDEX(col, '-', -1)) AS third
FROM mtl_categories_v
WHERE segment1 = '01';

在这里演示:

Rextester