Search code examples
sqloracle-databaseoracle9i

Substring between characters in Oracle 9i


In later versions I can use this regexp_substr:

SELECT 
    ID, 
    regexp_substr(ID, '[^.]+', 1, 2) DATA 1, 
    regexp_substr(ID, '[^.]+', 1, 3) DATA 2 
FROM employees

Table: Employees

ID
--------------------------
2017.1.3001-ABC.01.01
2017.2.3002-ABCD.02.02
2017.303.3003-ABC.03.03
2017.404.3004-ABCD.04.04

Expected output:

ID                       DATA 1 DATA 2
------------------------ ------ ---------
2017.1.3001-ABC.01.01    1      3001-ABC
2017.2.3002-ABCD.02.02   2      3002-ABCD
2017.303.3003-ABC.03.03  303    3003-ABC
2017.404.3004-ABCD.04.04 404    3004-ABCD

Please help me to get the sub-string between . characters in ID column in SQL Oracle 9i.


Solution

  • You don't need regular expressions - just use SUBSTR and INSTR:

    SELECT id,
           SUBSTR( id, dot1 + 1, dot2 - dot1 - 1) AS data1,
           SUBSTR( id, dot2 + 1, dot3 - dot2 - 1) AS data2
    FROM   (
      SELECT id,
             INSTR( id, '.', 1, 1 ) AS dot1,
             INSTR( id, '.', 1, 2 ) AS dot2,
             INSTR( id, '.', 1, 3 ) AS dot3
      FROM   employees
    );