【问题标题】:SQL Query to select a string after last delimiterSQL查询在最后一个分隔符之后选择一个字符串
【发布时间】:2020-12-21 06:41:47
【问题描述】:

我想在 ~ 分隔符最后出现后检索一个字符串。我有像“Attachments:Attachments~Attachment”这样的整个字符串,我想在 ~ 字符之后取子字符串,输出的字符将是 Attachment。在 SQL/Oracle select 语句中如何做到这一点?

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    使用REGEXP_SUBSTR

    select regexp_substr('Attachments:Attachments~Attachment','[^~]+$') from dual;
    
    • [^ ] - 用于指定一个不匹配的列表,您尝试在该列表中匹配除列表中的字符之外的任何字符。
    • + - 匹配一个或多个匹配项
    • $ - 匹配字符串的结尾

    Demo on db<>fiddle

    【讨论】:

      【解决方案2】:

      您可以使用substrinstr 来满足简单的模式匹配要求,因为与substrinstr 组合相比,regexp 的成本更高。

      您可以尝试以下方法:

      substr(str,instr(str,'~',-1) + 1)
      

      示例:

      SQL> select substr('Attachments:Attachments~Attachment1~Attachment2',
        2                instr('Attachments:Attachments~Attachment1~Attachment2','~',-1) + 1)
        3    from dual;
      
      SUBSTR('ATT
      -----------
      Attachment2
      
      SQL>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-23
        • 2020-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多