【问题标题】:How to get the value between 2 characters in oracle sql如何在oracle sql中获取2个字符之间的值
【发布时间】:2019-03-11 11:34:06
【问题描述】:

大家好,我的专栏中有这个值:

<?xml version="1.0" encoding="UTF-8"?><?dsk-revision 1.0?><Descriptor><pde><a><dov>784512</dov></a></pde></Descriptor>

我只会检索:

<dov>784512</dov>

你有什么想法吗?

谢谢!

【问题讨论】:

  • 到目前为止您尝试检索什么?
  • 你列的数据类型是XMLTYPE吗?
  • @Boneist 列类型是字符串,值是 XML 值 (。 ..) 但它被保存为字符串值。

标签: sql oracle substring


【解决方案1】:

你可以使用regexp_replace():

select x.*,
       regexp_replace(str, '<pde><a>(.*)</a></pde>', '\1')
from (select '<pde><a><dov>784512</dov></a></pde>' as str from dual) x;

regexp_substr():

select x.*,
       regexp_substr(str, '<dov>.*</dov>')
from (select '<pde><a><dov>784512</dov></a></pde>' as str from dual) x;

我不清楚你想要字符串是因为“dov”还是“pde/a”之间。

【讨论】:

    【解决方案2】:

    SUBSTR + INSTR:

    SQL> with test (col) as
      2  (select '<pde><a><dov>784512</dov></a></pde>' from dual)
      3  select substr(col, instr(col, '<dov>', 1, 1),
      4                     instr(col, '</dov>', 1, 1) + 6 - instr(col, '<dov>')) result
      5  from test;
    
    RESULT
    -----------------
    <dov>784512</dov>
    
    SQL>
    

    【讨论】:

      【解决方案3】:

      使用带有指定掩码的 REGEXP_REPLACE:

      select x.*,
             regexp_replace(str, '.*<a>(.*)</a>.*', '\1'), -- anything between <a> </a>
             regexp_replace(str, '.*(<dov>.*</dov>).*', '\1'), -- anything between <dov> </dov> tag (tag included)
             regexp_replace(str, '.*<pde><a>(.*)</a></pde>.*', '\1'), -- anything between <pde><a> </a></pde>
             regexp_replace(str, '.*<pde><a>(<dov>.*</dov>)</a></pde>.*', '\1') -- anything between <pde><a> </a></pde> that is inside <dov> </dov> tag (tag included)
      from (select '<pde><a><dov>784512</dov></a></pde>' as str from dual) x;
      

      【讨论】:

        猜你喜欢
        • 2015-04-01
        • 2021-11-10
        • 2016-02-12
        • 2013-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-06
        • 1970-01-01
        相关资源
        最近更新 更多