2017-02-04 126 views
1

我有這樣一個字符串這分割字符串基於連字符

XX0099-X01 

我想基於連字符的字符串,一分爲二,即 XX0099X01

我曾嘗試爲

SELECT 'XX0099-X01', 
      SUBSTR('XX0099-X01', 
        1, 
        INSTR('XX0099-X01', 
         '-', 
         1 
         ) -1 
       ) 
    FROM dual 

不確定如何獲得第二部分。

我們有一箇舊的遺留系統,它仍然使用8i Oracle數據庫。

回答

0

使用的substr組合instr

select 
    substr(s, 1, instr(s, '-') - 1), 
    substr(s, instr(s, '-') + 1) 
from t; 

如果支持的另一種方法是使用regexp_substr

select 
    regexp_substr('XX0099-X01','[^-]+', 1, 1), 
    regexp_substr('XX0099-X01','[^-]+', 1, 2) 
from dual; 
+0

我們有一個ol d仍然使用8i Oracle數據庫的遺留系統,以上不在8i中工作。謝謝 – user75ponic

+0

@ user75ponic - 爲舊版本添加了更新 – GurV

+0

非常感謝,非常感謝。 – user75ponic

2

如果您不能使用正則表達式串/更換,這裏是一個選項,使用Oracle的基本字符串函數:

SELECT SUBSTR(col, 1, INSTR(col, '-') - 1) AS first_part, 
     SUBSTR(col, INSTR(col, '-') + 1) AS second_part 
FROM yourTable 
+0

非常感謝,非常感謝。 – user75ponic