2017-06-20 256 views
0

我在Oracle數據庫中的列具有這樣的數據拆分列在甲骨文

column1 
/opt/log/data/abcd.efghi.jklmn.aaa.txt 
/opt/log/data/abbbcd.efccghi.jkdsdflmn.abab.txt 
/opt/log/data/nmvcnmcd.efjhjghi.jkvslmn.abcbc.txt 
/opt/log/data/hjsdhj.hjfdhdf.hdfhjd.aghag.txt 
/opt/log/data/dfhjfdhj.yureyer.qwtyq.hjahjh.txt 

我想在這樣的方式

**firstdot seconddot  thirdnfourthdot** 
abcd  efghi   jklmn.aaa 
abbbcd  efccghi  jkdsdflmn.abab 
nmvcnmcd efjhjghi  jkvslmn.abcbc 
hjsdhj  hjfdhdf  hdfhjd.aghag 
dfhjfdhj yureyer  qwtyq.hjahjh 

我能得到的數據拆分第二點值由

select substr(column1,instr(column1,'.',1+1,instr(column1,'.',1,2)-instr(column1,'.',1,1)-1) as secondot 

但我沒有得到休息。你們能幫忙嗎?

非常感謝

回答

1

沒有正則表達式,你需要回復同樣的邏輯爲你需要的每個子串,每個timi根據th的位置選擇初始位置和leght該子字符串的e「終止符」。

/* input data */ 
    with yourTable(column1) as (
     select '/opt/log/data/abcd.efghi.jklmn.aaa.txt' from dual union all 
     select '/opt/log/data/abbbcd.efccghi.jkdsdflmn.abab.txt' from dual union all 
     select '/opt/log/data/nmvcnmcd.efjhjghi.jkvslmn.abcbc.txt' from dual union all 
     select '/opt/log/data/hjsdhj.hjfdhdf.hdfhjd.aghag.txt' from dual union all 
     select '/opt/log/data/dfhjfdhj.yureyer.qwtyq.hjahjh.txt' from dual 
    ) 
/* query */ 
    select substr(column1, instr(column1, '/', -1) +1, instr(column1, '.') - instr(column1, '/', -1)-1) firstDot, 
      substr(column1, instr(column1, '.') +1, instr(column1, '.', 1, 2) - instr(column1, '.') -1) secondDot, 
      substr(column1, instr(column1, '.', 1, 2) +1, instr(column1, '.', 1, 4) - instr(column1, '.', 1, 2) -1) thirdAndFourthDot 
    from yourTable 

給出:

FIRSTDOT  SECONDDOT  THIRDANDFOURTHD 
--------------- --------------- --------------- 
abcd   efghi   jklmn.aaa 
abbbcd   efccghi   jkdsdflmn.abab 
nmvcnmcd  efjhjghi  jkvslmn.abcbc 
hjsdhj   hjfdhdf   hdfhjd.aghag 
dfhjfdhj  yureyer   qwtyq.hjahjh 

在一個更可讀的方式:

select substr(column1, lastSlashPos +1, firstDotPos - lastSlashPos -1) as firstDot, 
     substr(column1, firstDotPos +1, secondDotPos - firstDotPos -1) as secondDot, 
     substr(column1, secondDotPos +1, fourthDotPos - secondDotPos -1) as thirdAndFourthDot 
from (
     select instr(column1, '/', -1) as lastSlashPos, 
       instr(column1, '.') as firstDotPos, 
       instr(column1, '.', 1, 2) as secondDotPos, 
       instr(column1, '.', 1, 3) as thirdDotPos, 
       instr(column1, '.', 1, 4) as fourthDotPos, 
       column1 
     from yourTable 
    ) 
+0

感謝這個作品 – user3845185

0
select substr('/opt/log/data/abcd.efghi.jklmn.aaa.txt',instr('/opt/log/data/abcd.efghi.jklmn.aaa.txt','/',-1) + 1) from dual; 

這會給你以後最後/

文本,然後你需要申請instr.

select 
    substr(text, 1, instr(text,'.', 1) - 1), 
    substr(text, instr(text,'.', 1) + 1, instr(text,'.', 2) - 1), 
    substr(text, instr(text,'.', 2) + 1) 
from (
    select substr('/opt/log/data/abcd.efghi.jklmn.aaa.txt',instr('/opt/log/data/abcd.efghi.jklmn.aaa.txt','/',-1) + 1) text from dual 
); 
+0

是否有可能做到這一點,而無需使用SUBSTR函數的聲明從? – user3845185

+0

但這看起來更清楚。 – Kacper