【发布时间】:2020-12-21 06:41:47
【问题描述】:
我想在 ~ 分隔符最后出现后检索一个字符串。我有像“Attachments:Attachments~Attachment”这样的整个字符串,我想在 ~ 字符之后取子字符串,输出的字符将是 Attachment。在 SQL/Oracle select 语句中如何做到这一点?
【问题讨论】:
我想在 ~ 分隔符最后出现后检索一个字符串。我有像“Attachments:Attachments~Attachment”这样的整个字符串,我想在 ~ 字符之后取子字符串,输出的字符将是 Attachment。在 SQL/Oracle select 语句中如何做到这一点?
【问题讨论】:
select regexp_substr('Attachments:Attachments~Attachment','[^~]+$') from dual;
[^ ] - 用于指定一个不匹配的列表,您尝试在该列表中匹配除列表中的字符之外的任何字符。+ - 匹配一个或多个匹配项$ - 匹配字符串的结尾【讨论】:
您可以使用substr 和instr 来满足简单的模式匹配要求,因为与substr 和instr 组合相比,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>
【讨论】: