oracle split逗号分隔字符串到

时间:2015-10-13 14:54:40

标签: sql oracle oracle11g

我有一张像

这样的表格
Charge_num      mapping_col
---------       -----------
p1.pm.100       1.1.1,1000
p1.pm.110       1.2.1,1000
p1.pm.120       1.3.1,1000

我需要将“mapping_col”拆分为2列,如下所示:

Charge_num      wbs       obs
---------       ---       ---
p1.pm.100       1.1.1     1000
p1.pm.110       1.2.1     1000
p1.pm.120       1.3.1     1000

2 个答案:

答案 0 :(得分:1)

select charge_num, 
substr(mapping_col,1,instr(mapping_col,',',1)-1) AS first_half,
substr(mapping_col, instr(mapping_col,',',1)+1) as last_half
from your_table

请注意,将数字存储为字符串的做法很容易出错。如果您将这些结果转换为NUMBER(9),如果您的数据不规则,则可能无法预测

答案 1 :(得分:1)

REGEXP_SUBSTR救援!

with sample_data as (select 'p1.pm.100' charge_num, '1.1.1,1000' mapping_col from dual union all
                     select 'p1.pm.110' charge_num, '1.2.1,1000' mapping_col from dual union all
                     select 'p1.pm.120' charge_num, '1.3.1,1000' mapping_col from dual)
select charge_num,
       regexp_substr(mapping_col, '[^,]+', 1, 1) wbs,
       regexp_substr(mapping_col, '[^,]+', 1, 2) obs
from   sample_data;

CHARGE_NUM WBS                            OBS                           
---------- ------------------------------ ------------------------------
p1.pm.100  1.1.1                          1000                          
p1.pm.110  1.2.1                          1000                          
p1.pm.120  1.3.1                          1000