在方括号内对字符进行分组,而不是”Like”或”Like”。

huangapple go评论47阅读模式
英文:

Grouping of characters inside the square bracket instead Like or Like

问题

I write this:

...
where
code LIKE 'TT[ADGWEK]%'

但我不想要这段代码返回,例如 'TTAK' 或 'TTEK'。

英文:

How can I summarize this code?

...
where
code LIKE 'TTAD%'
or
code LIKE 'TTAG%'
or
code LIKE 'TTAW%'
or
code LIKE 'TTE%'
or
code LIKE 'TTK%'

I write this:

...
where
Code LIKE 'TT[AD,AG,AW,E,K]%'

but, I don't want this code returns, for example, 'TTAK' or 'TTEK'.

答案1

得分: 3

SQL Server 不原生支持正则表达式,所以我们可能最好能够使用增强的 LIKE 操作符来实现:

WHERE code LIKE 'TTA[DGW]%' OR code LIKE 'TT[EK]%'

请注意,上述实际上匹配了 TTEK。如果你想要排除它,可以使用:

WHERE (code LIKE 'TTA[DGW]%' OR code LIKE 'TT[EK]%') AND   -- whitelist
      code NOT LIKE 'TTEK%'   -- blacklist
英文:

SQL Server does not natively support regex, so the best we can probably do using the enhanced LIKE operator is:

<!-- language: sql -->

WHERE code LIKE &#39;TTA[DGW]%&#39; OR code LIKE &#39;TT[EK]%&#39;

Note that the above does in fact match TTEK. If you want to exclude that, you use:

<!-- language: sql -->

WHERE (code LIKE &#39;TTA[DGW]%&#39; OR code LIKE &#39;TT[EK]%&#39;) AND   -- whitelist
      code NOT LIKE &#39;TTEK%&#39;   -- blacklist

huangapple
  • 本文由 发表于 2023年5月13日 16:10:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定