如何获取oracle sql中2字符串之间的值

时间:2015-02-02 10:59:08

标签: sql string oracle substring clob

enter image description here我有一个SOAP_MONITORING表,它有一个RESPONSE_XML列,它是CLOB数据类型,由大字符串组成。我需要获取并显示隐藏在此字符串中的所有SUBSCRIPTION_ID。 SUBSCRIPTION_ID位于此字符串中:<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>201411211617575057</ax2130:id>。我必须得到所有ID,这只是我的SUBSCRIPTION_ID,它位于<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id> and </ax2130:id> string之间。我尝试了以下查询:

   SELECT REPLACE(REPLACE(MatchedId, '<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>', ''), '</ax2130:id>', '') 
FROM
(
SELECT   REGEXP_SUBSTR(RESPONSE_XML, '<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>\d+</ax2130:id>') 
FROM  SOAP_MONITORING 
)
WHERE 
WEB_SERVICE_NAME='RatorWebShopService' and WEB_METHOD_NAME='placeShopOrder'

但收到了空洞的结果。

3 个答案:

答案 0 :(得分:2)

我认为这是正则表达式可以帮助您的情况。 here是oracle文档的参考。

正则表达式可能类似于:

<ax2130:id>\d+</ax2130:id>

如果您的ID仅为数字。

更新

以下是您可以使用的示例查询:

SELECT REPLACE(REPLACE(MatchedId, '<ax2130:id>', ''), '</ax2130:id>', '') AS CleanMatch
FROM
(
    SELECT   REGEXP_SUBSTR(RESPONSE_XML, '<ax2130:id>\d+</ax2130:id>') AS MatchedId
    FROM     SOAP_MONITORING
)
WHERE 
MatchedId is not null

http://sqlfiddle.com/#!4/d473c/5

答案 1 :(得分:0)

CLOB无法像varchar那样以多种方式处理如果您的列包含XML,则应考虑使用Oracles xmltype数据类型。在这种情况下,您可以使用XML-Tools查询您的数据:即

select extractvalue(response_xml, '/response_xml/id/text()')
from soap_monitoring
order by  extractvalue(response_xml, '/response_xml/id/text()') desc

答案 2 :(得分:0)

请检查下面的编辑问题

 with unique_data as (select '<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO">' data from dual)

    select  SUBSTR(MatchedId,instr(MatchedId,'>')+1 , instr(MatchedId,'</') - length('</ax2130:id>')) from
    (
    SELECT   replace(dbms_lob.substr(RESPONSE_XML,length(response_xml),dbms_lob.instr(response_xml,data)),data,'') AS MatchedId
    FROM     SOAP_MONITORING,unique_data
    )
    WHERE 
    MatchedId is not null;