仅在某些条件下选择 Oracle SQL 中字符串中特定单词后面的子字符串

问题描述 投票:0回答:1

跟进另一位用户的帖子,标题为“如何在 Oracle SQL 中选择字符串中特定单词之后的子字符串?”

我正在使用 Oracle SQL Developer 来运行我的脚本。

该解决方案在我的 50% 场景中完美运行,使用:

ltrim(substr(SUMMARY, instr(SUMMARY, 'TEXT:') + length('TEXT:')))  AS Summary

当我的系统生成警报并且字符串一致(始终包含“TEXT:”)时,此功能有效 然而,在某些情况下需要手动输入字符串。在这些情况下,我需要输出整个字符串/句子。

例如:

示例 输出
文本:网关没有响应 网关无响应
上次事件的后续 从之前的事件中恢复过来

有人知道我怎样才能做到这一点吗?

substring oracle-sqldeveloper
1个回答
0
投票

一种选择是有条件地进行;如果文本以

TEXT:
开头,请将其删除并返回消息的其余部分。否则,返回整个消息。

样本数据:

SQL> with test (col) as
  2    (select 'TEXT: No response from gateway'   from dual union all
  3     select 'Follow up from previous incident' from dual
  4    )

查询:

  5  select col,
  6         case when substr(col, 1, 5) = 'TEXT' then substr(col, 6)
  7              else col
  8         end result
  9  from test;

COL                              RESULT
-------------------------------- --------------------------------
TEXT: No response from gateway   TEXT: No response from gateway
Follow up from previous incident Follow up from previous incident

SQL>
© www.soinside.com 2019 - 2024. All rights reserved.