frosty
frosty

Reputation: 5370

finding exact strings with sql like statement

I have the following statement

SELECT *
FROM   Delivery_Note this_
WHERE  mycol like '%[126]%'

I've noticed this also returns rows that contain [123]. What is the best way to find exact matching in a string.

edit I appreciate this is not an efficient query. There a few other reasons for this approach.

Upvotes: 0

Views: 2861

Answers (1)

Aaron Bertrand
Aaron Bertrand

Reputation: 280252

Brackets specify a range when used with LIKE. You can use the ESCAPE keyword.

WHERE mycol LIKE '%\[126\]%' ESCAPE '\';

Of course if you are trying to match an exact string, you don't need LIKE, or you can drop the % characters and LIKE will behave like = (this makes it flexible to pass in wildcards or exact matches to a parameter).

Upvotes: 2

Related Questions