SQL Server How to use like with [] values in data in sql [duplicate]

w8f9ii69  于 2023-03-22  发布在  其他
关注(0)|答案(1)|浏览(151)

This question already has answers here:

How can I escape square brackets in a LIKE clause? (10 answers)
Closed 4 days ago.

select * 
from Table1 
where Email = 'test()<>[]:;@\\@test.com' 

select * 
from Table1 
where Email like '%test()<>[]:;@\\@test.com%'

The first query is returning data, while the second query is not. How can we use like in this query?

The second query should return the same data as the first.

u2nhd7ah

u2nhd7ah1#

As @siggemannen has rightly pointed out : (I will use his original sentence from above comment):

[] is part of the LIKE pattern matching . which matches empty set of characters. To be able to use such characters, you have to ESCAPE them with a escape clause.

So, Your query can be written as :

SELECT * FROM Table1 WHERE Email LIKE '%test()<>\[\]:;@\\\\@test.com%' ESCAPE '\';

Here is a demo using dbfiddle

相关问题