值中的特殊字符不适用于 like 运算符,而同样适用于 SQL 服务器中的 = 运算符

Special characters inside the value does not work with like operator while the same works with = operator in SQL Server

谁能帮我找到值

的问题
City!@#$%^&*()_+-={}|:"<>?;''./[]\/*-+.

存储在触发 select 查询的列中,与 = 运算符一起工作正常,但在与 LIKE 运算符一起使用时不起作用。

例如:

create table test_kee (some_column nvarchar(50));

insert into test_kee 
values ('City!@#$%^&*()_+-={}|:"<>?;''./[]\/*-+.');

此查询工作正常:

 select * 
 from test_kee 
 where some_column = 'City!@#$%^&*()_+-={}|:"<>?;''./[]\/*-+.';

但是这个查询不起作用:

 select * 
 from test_kee 
 where some_column like '%City!@#$%^&*()_+-={}|:"<>?;''./[]\/*-+.%';

字符[]%_like 运算符有特殊含义。
如果您使用 like 运算符,则需要以不同方式处理它们。
具体来说,您需要使用随后在 escape 子句中指定的字符对它们进行转义:

select * 
from test_kee 
where some_column like '%City!@#$~%^&*()~_+-={}|:"<>?;''./~[~]\/*-+.%' escape '~';

official documentation

中提供了更多详细信息