Search code examples
oracle-databaseclobregexp-like

Oracle SQL to list all occurrences of string in Clob/Blob Field


I have a field in my Oracle table "Table1" with Column "texta" which has all my codes written. I would like to extract all the occurrences which matches the patter "record." or "Record." and list the result. For example:-

Content of texta

CreateRecord = CreateRecord(Record.PS_JOB)
CreateRecord = CreateRecord(Record.PS_NATIONAL_ID)

The expected result is

PS_JOB
PS_NATIONAL_ID

Solution

  • See if this helps.

    SQL> desc test
     Name                                                  Null?    Type
     ----------------------------------------------------- -------- -------------------
     ID                                                             NUMBER
     TEXTA                                                          CLOB
    
    SQL> select * From test;
    
            ID TEXTA
    ---------- ------------------------------------------------------------------------
             1 CreateRecord = CreateRecord(Record.PS_JOB)
               CreateRecord = CreateRecord(Record.PS
    
             2 CreateRecord = CreateRecord(Record.ABC_DEF)
             3 CreateRecord = CreateRecord(Record.LITTLE_FOOT)
    
    SQL> select id,
      2    regexp_substr(texta, 'Record\.\w+', 1, column_value) result
      3  from test cross join
      4       table(cast(multiset(select level from dual
      5                           connect by level <= regexp_count(texta, 'Record\.')
      6                          ) as sys.odcinumberlist));
    
            ID RESULT
    ---------- ------------------------------------------------------------------------
             1 Record.PS_JOB
             1 Record.PS_NATIONAL_ID
             2 Record.ABC_DEF
             3 Record.LITTLE_FOOT
    
    SQL>