【问题标题】:Searching for specific values in a string SQL在字符串 SQL 中搜索特定值
【发布时间】:2021-12-02 02:58:10
【问题描述】:

我需要在字符串中搜索可能出现在其中任何位置(包括开头)的特定值。它必须是完全匹配的。

select * from LeadTimeNodes where ParentCombination like '%293 %'

所以我需要在 ParentCombination 字段中找到 293 作为它自己的实体。

字段中的一些字符串示例:

'3293 >>> 671' should not find anything as 293 is part of a bigger number 3293

'293' should find it as it is on its own

'479 >>> 12930' should not find anything as 293 is part of 12930

'6000 >>> 293' should find it

'293 >>> 1600' should find it

如果我使用上面的示例代码,它会将空格合并到搜索中并在之后的数字中工作,但是如果我在它之前放置一个空格 '% 293 %' 则它将找不到以 293 开头的字符串

希望这是有道理的。

【问题讨论】:

  • 您需要为此使用正则表达式。 ¿ 你的数据库是什么?其他不太优雅的解决方案是 likesors 的组合:ParentCombination like '293 %' OR ParentCombination like '% 293 %' OR ParentCombination like '% 293'
  • 什么是数据库?如果引擎支持它们,也许可以使用正则表达式来完成。
  • 抱歉,我应该说,这是一个 SQL Server 数据库。

标签: sql string search


【解决方案1】:

我选择了 James 的 Like & Or 组合解决方案。可能不是最雄辩的,但它成功了。 谢谢

【讨论】:

  • 请不要发布更新您问题的答案,而是标记正确答案或更新您的问题以包含您在无法标记已接受答案时采取的步骤。
【解决方案2】:

在甲骨文中:

select REGEXP_Replace('3293 >>> 671', '(.*) >>> (.*)', '\1') as first_param,
REGEXP_Replace('3293 >>> 671', '(.*) >>> (.*)', '\2') as second_param
from dual;

这会将输出作为两个单独的值。然后你可以用完全匹配的字符串将它包装在另一个选择中。

【讨论】:

  • 为什么不是WHERE REGEXP_LIKE
  • 因为您将使用 regexp_like 添加多个 OR 条件,这意味着使用 regex 匹配三次。
猜你喜欢
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多