2014-03-26 58 views
0

我試圖從使用Oracle SQL的REGEXP_SUBSTR與clob中提取多個字符串。SQL Clob搜索Clob中的多個字符串並加入,可能REGEXP_SUBSTR

clob_field例如:

xxx xxx"xxxxxxxYY=1234xxxxx.xxxx.xxxx"xx  xxxxxxxxxxxxxx 
xxxxx"xxxxxxxYY=2345xxxxx.xxxxx.xxxxxxxxxxxx"xxxx xxxxxxxxxx 
xxx xxxxx"xxxxxxxxxxxxxxYY=34567xxxxx.xxx.xxxxx"xxxxxxxxxxxx 
xxxx"xxxxxYY=4567xx.xxxxxx"xxxxxxxxxx xxxxxxxxxxx xxxxxxxx 

我已經試過:

select a.id, regexp_substr(b.clob_field,'YY=',",[^"]+"') "clob_result" 
from table1 a, table2 b 
where a.id = b.id 

從連接查詢例如期望的結果是:

id  clob_result 
1  xxxxxxxYY=1234xxxxx.xxxx.xxxx 
1  xxxxxxxYY=2345xxxxx.xxxxx.xxxxxxxxxxxx 
1  xxxxxxxxxxxxxxYY=34567xxxxx.xxx.xxxxx 
1  xxxxxYY=4567xx.xxxxxx 

不清楚如何搜索包含「STRING YY =',然後返回整個字符串「開頭和結尾」

任何幫助,非常感謝!

回答

0

您可以使用REGEXP_REPLACE獲得多次出現,與周圍的雙引號一起。然後,您可以使用TRIM/REPLACE刪除它們。

SQL Fiddle

with x(y) as (
    select 'xxxx"xxxxxxxYY=1234xxxxx.xxxx.xxxx"xx  xxxxx' from dual union all 
    select 'xx"xxxxxxxYY=2345xxxxx.xxxxx.xxxxxxxxxxxx"xxx xxx' from dual union all 
    select 'xxx"xxxxxxxxxxxxYY=34567xxxxx.xxx.xxxxx"xxxxxxxxx' from dual union all 
    select 'x"xxxxxYY=4567xx.xxxxxx"xx"xxxxxYY=8787xx.xxxx"xx' from dual 
    ) 
select y, replace(trim('"' from regexp_replace(y, 
               '("[^"]*YY=[^"]*")|(.)', 
               '\1' 
              ) -- this will remove anything that is not matched by the regex 
        ), 
        '""', 
        ',' 
       ) as clob_result 
from x 

Results

|             Y |        CLOB_RESULT | 
|---------------------------------------------------|-------------------------------------------| 
| xxxx"xxxxxxxYY=1234xxxxx.xxxx.xxxx"xx  xxxxx |    xxxxxxxYY=1234xxxxx.xxxx.xxxx | 
| xx"xxxxxxxYY=2345xxxxx.xxxxx.xxxxxxxxxxxx"xxx xxx | xxxxxxxYY=2345xxxxx.xxxxx.xxxxxxxxxxxx | 
| xxx"xxxxxxxxxxxxYY=34567xxxxx.xxx.xxxxx"xxxxxxxxx |  xxxxxxxxxxxxYY=34567xxxxx.xxx.xxxxx | 
| x"xxxxxYY=4567xx.xxxxxx"xx"xxxxxYY=8787xx.xxxx"xx | xxxxxYY=4567xx.xxxxxx,xxxxxYY=8787xx.xxxx | 
+0

謝謝你的評論Peach,又是Egor。 – Temu

0
select 
    regexp_substr(string,'"([^"]*YY=[^"]*)"', 1, 1, '', 1) as clob_result 
from your_table 

fiddle


select 
    id, occ, 
    regexp_substr(string,'"([^"]*YY=[^"]*)"', 1, occ, '', 1) as clob_result 
    from your_table, 
     (select level occ from dual connect by level < 99) 
    where occ <= regexp_count(string,'"([^"]*YY=[^"]*)"') 
    order by id, occ 

fiddle

+0

謝謝葉戈爾!這很好,但只返回第一次出現。任何建議返回所有出現的'想要的結果'的例子? – Temu

+0

@ user3466045 - 附加。 –