Search code examples
sqlregexoracle-databasesplitoracle9i

REGEXP_SUBSTR function equivalent in Oracle 9i?


Is there an equivalent function for regexp_substr in Oracle 9i.

I know regexp_substr came in Oracle 10g onwards.

Trying to figure out a way if I can use the same function logic in Oracle 9i.

I have data like

0/6/03/19
0/6/3/19
0/1/3/09

I want to pick out the values separately by delimiting the string with / .

I tried using substr and instr but it won't be generic as the length of the string between slashes can change.


Solution

  • Unfortunately you can only use instr and substr combination as :

    with t as
    (
      select '0/6/03/19' as str from dual union all
      select '0/6/3/19' from dual
    )
    select substr(str,1,instr(str,'/',1,1)-1) str1,
           substr(str,instr(str,'/',1,1)+1,instr(str,'/',1,2)-instr(str,'/',1,1)-1) str2,
           substr(str,instr(str,'/',1,2)+1,instr(str,'/',1,3)-instr(str,'/',1,2)-1) str3,
           substr(str,instr(str,'/',1,3)+1,length(str)-instr(str,'/',1,3)) str4
      from t;
    
    STR1    STR2    STR3    STR4
    ----    ----    ----    ----
     0       6       03      19
     0       6        3      19
    

    P.S. If your DB's version is 9.2 then with .. as structure may be used as above.

    you can also get the results row-wise(in unpivot manner) as :

    with t as
    (
     select '/'||str||'/' as str, ID
       from
       (
        select 1 as ID, '0/6/03/19' as str from dual union all
        select 2,'0/6/3/19' from dual
       )
    )
    select
          distinct ID, level as piece_nr,
          substr(str,instr(str,'/',1,level)+1,instr(str,'/',1,level+1)-instr(str,'/',1,level)-1)
          as piece_value
      from ( select * from t )
    connect by level <= length(str)-length(replace(str,'/',''))-1
      order by ID, level;
    
    ID  PIECE_NR    PIECE_VALUE
    --  --------  -----------
    1        1           0
    1        2           6
    1        3           03
    1        4           19
    2        1           0
    2        2           6
    2        3           3
    2        4           19