SQL Server Grouping of characters inside the square bracket instead Like or Like

ruarlubt  于 2023-05-16  发布在  其他
关注(0)|答案(1)|浏览(120)

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'.

eqoofvh9

eqoofvh91#

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

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:

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

相关问题