簡體   English   中英

在Oracle中基於REGEXP_INSTR從字符串中提取多個值

[英]extract multiple values from string based on REGEXP_INSTR in Oracle

HI有以下示例字符串。

申領編號:299765成員:JOHNSON,XYZ服務熱線1
行動代碼0響應指令1641800532咨詢專線2服務專線
2號操作代碼0響應指令400程序代碼
4805587'

當在整個字符串中標識出響應指令字符串時,我需要在響應指令字符串之后提取值。

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; 

現在,根據上述查詢,我​​只得到一個結果

NUM_VAL

1641800532

預期結果集

NUM_VAL

1641800532

400

請多次幫助。 謝謝

有兩種簡單的方法。

A)確定要支持的最大可能出現次數

在這種方法中,將輸入數據重復50次,然后在第一個重復項中搜索第一個匹配項,在第二個重復項中搜索第二個匹配項,依此類推。

輸入數據中不存在的任何事件都將為null ,因此您需要在WHERE子句中將其過濾掉。

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)使用LATERAL內聯視圖(僅適用於Oracle 12c或更高版本)

此版本將支持任何數量的事件,並且應具有更好的性能,因為您不會將數據重復50次,只能發現一個或兩個事件。 不幸的是,它在您說的11g中不起作用。 我包括它只是為了完整性。

-- 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
;

使用標准的connect by level查詢:

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;

輸出

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

2 rows selected.

這只會在“響應指令”之后捕獲NUMBERS。 如果您確實要這么做,則可以改回\\w+ (而不是\\d+ )。 它還在“響應指令”之后只留一個空格-如果願意,可以改回\\s+

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM