简体   繁体   中英

extract multiple values from string based on REGEXP_INSTR in Oracle

HI have following sample string.

'Claim Number: 299765Member: JOHNSON,XYZ Service Line Number 1
Action Code 0 Response Directive 1641800532 Advice Line 2 Service Line
Number 2 Action Code 0 Response Directive 400 Procedure Code
4805587'

I need to extract the values after Response Directive string when ever there is a Response Directive String identified in the whole string .

WITH TEST AS(
SELECT 'Claim Number: 299765Member: JOHNSON,XYZ  Service Line Number 1                
Action Code 0    Response Directive 1641800532    Advice Line 2 Service Line         
Number 2    Action Code 0    Response Directive 400    Procedure Code   
4805587'
AS NOTE_TEXT FROM DUAL
)
SELECT regexp_substr(NOTE_TEXT,'response directive+\s+(\w+)',1,1,'i',1) as       
NUM_VAL
FROM TEST; 

Right now I am resulted with only one result based on above query I written

NUM_VAL

1641800532

Expected Result Set

NUM_VAL

1641800532

400

Kindly help for multiple Occurrences. Thank You

There are two easy ways.

A) Decide on a maximum number of possible occurrences to support

In this approach, duplicate the input data say, 50 times, and search the 1st duplicate for the 1st occurrence, the 2nd duplicate for the 2nd occurrence, etc.

Any occurrence that doesn't exist in the input data will be null , so you will need to filter those out in the WHERE clause.

WITH TEST AS(
SELECT 'Claim Number: 299765Member: JOHNSON,XYZ  Service Line Number 1                
Action Code 0    Response Directive 1641800532    Advice Line 2 Service Line         
Number 2    Action Code 0    Response Directive 400    Procedure Code   
4805587'
AS NOTE_TEXT FROM DUAL
),
occurrences AS ( SELECT rownum occurrence# FROM dual connect by -- Max 50 occurrences supported
rownum <= 50 )  
SELECT regexp_substr(NOTE_TEXT,'response directive+\s+(\w+)',1,occurrences.occurrence#,'i',1) as       
NUM_VAL
FROM test cross join occurrences
WHERE regexp_substr(NOTE_TEXT,'response directive+\s+(\w+)',1,occurrences.occurrence#,'i',1) IS NOT NULL;

B) Use a LATERAL inline view (only works in Oracle 12c or later)

This version will support any number of occurrences and should perform better since you are not duplicating the data 50 times to only find one or two occurrences. Unfortunately, it does not work in 11g, which you said you are on. I'm including it only for completeness.

-- 12c version
WITH TEST AS(
SELECT 'Claim Number: 299765Member: JOHNSON,XYZ  Service Line Number 1                
Action Code 0    Response Directive 1641800532    Advice Line 2 Service Line         
Number 2    Action Code 0    Response Directive 400    Procedure Code   
4805587'
AS NOTE_TEXT FROM DUAL
)
SELECT regexp_substr(NOTE_TEXT,'response directive+\s+(\w+)',1,occurrences.occurrence#,'i',1) as       
NUM_VAL
FROM test cross join lateral ( SELECT rownum occurrence# FROM dual connect by -- Max 50 occurrences supported
rownum <= regexp_count(note_text, 'response directive+\s+(\w+)',1,'i') ) occurrences
;

Using a standard connect by level query:

with 
     test (note_text) as (
       select 'Claim Number: 299765Member: JOHNSON,XYZ  Service Line Number 1
               Action Code 0    Response Directive 1641800532    Advice Line 2 Service Line
               Number 2    Action Code 0    Response Directive 400    Procedure Code   
               4805587'
       from    dual
     )
select  level as lvl,
        regexp_substr(note_text,'response directive (\d+)', 1, level, 'i', 1) as num_val
from    test
connect by level <= regexp_count(note_text, 'response directive \d+', 1, 'i')
order by lvl;

Output :

  LVL NUM_VAL
----- ----------
    1 1641800532
    2 400

2 rows selected.

This will only catch NUMBERS following "response directive". You may change back to \\w+ (instead of \\d+ ) if you really meant that. It also allows for exactly one space after "responsive directive" - you may change back to \\s+ if you prefer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM