英文:
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 'TTA[DGW]%' OR code LIKE 'TT[EK]%'
Note that the above does in fact match TTEK
. If you want to exclude that, you use:
<!-- language: sql -->
WHERE (code LIKE 'TTA[DGW]%' OR code LIKE 'TT[EK]%') AND -- whitelist
code NOT LIKE 'TTEK%' -- blacklist
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论