簡體   English   中英

oracle將逗號分隔的字符串拆分為列

[英]oracle split comma delimited string into columns

我有一張桌子

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”分成兩列,如下所示:

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
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),如果數據不規則,則可能會意外中斷

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          

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM