根据Oracle

时间:2016-07-06 21:44:55

标签: sql oracle oracle11g

HI有以下示例字符串。

'索赔号:299765会员:JOHNSON,XYZ服务热线1号     操作代码0响应指令1641800532建议2行服务线
    2号行动代码0响应指令400程序代码
    4805587'

当需要在整个字符串中标识出响应指令字符串时,我需要在Response Directive字符串后提取值。

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

请多次出现帮助。谢谢

2 个答案:

答案 0 :(得分:0)

有两种简单的方法。

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
;

答案 1 :(得分:0)

使用标准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+