[英]How to handle special characters in SQL Server Select query
I have following data in table 我表中有以下数据
create table Manager(id int, managerid varchar(10) , managername varchar(50))
insert into Manager(id, managerid, managername)
values (1, 'A1', 'Mangesh'), (2, 'A''2', 'Sagar'), (3, '_C[%]3', 'Ah_mad'),
(4, 'A4', 'Mango'), (5, 'B5', 'Sandesh')
I am using stored procedure to get the result on the basis of given characters. 我正在使用存储过程来基于给定字符获取结果。 One of the users want to search with
c[%]
. 其中一位用户想要使用
c[%]
进行搜索。 I am able to handle %
with [%]
but if string is having [%]
then I am unable to find it. 我可以用
[%]
处理%
,但是如果字符串具有[%]
则无法找到它。
I tried with following query 我尝试了以下查询
declare @str varchar(100)='C[[%]'
SELECT m.*
FROM Manager m
WHERE m.managerid LIKE '%'+@str+'%'
This way I am able to get the result but in this case input is C[%
only. 这样我就可以得到结果,但是在这种情况下,输入仅是
C[%
。 I replaced %
with [%]
in my stored procedure parameter. 我在存储过程参数中用
[%]
替换了%
。 But if input is C[%]
then my query is not returning anything. 但是,如果输入为
C[%]
则我的查询未返回任何内容。
You have to escape the square bracket and %
character using []
, this tells SQL to treat them as literals. 您必须使用
[]
转义方括号和%
字符,这告诉SQL将它们视为文字。
So in order to find the %
it needs escaped as [%]
, the same for the square bracket it needs escaped as [[]
因此,为了找到需要转义的
%
为[%]
,对于需要将其转义为[[]
的方括号也是如此
You don't need to escape the ]
because if it's not paired with [
has no special meaning. 您不需要转义[
]
因为如果未与[
]
配对,则没有特殊含义。
select * from Manager where managerid like '%c[[][%]]%'
Use Escape
to ignore the wildcards
. 使用
Escape
忽略wildcards
。 Try this 尝试这个
select * from Manager
where managerid like '%c\[%]%' escape '\'
您只需要转义一个开头的'['
SELECT * FROM MANAGER where managerid like '%_C[[%]%'
Both %
, _
and [
are special characters and should be escaped. %
, _
和[
都是特殊字符,应转义。 So it could be that even your INSERT-statement is wrong. 因此,即使您的INSERT语句也可能是错误的。
You can place the special characters between square brackets [
and ]
to make them literal. 您可以在方括号
[
和]
之间放置特殊字符以使它们成为文字。
insert into Manager(id,managerid,managername)values(3,'[_]C[[][%]]3','Ah[_]mad')
You can use this same method to search on: 您可以使用相同的方法进行搜索:
SELECT * FROM Manager
WHERE managerid LIKE '%C[[][%]]%'
Or define your own escape character like this: 或像这样定义自己的转义字符:
SELECT * FROM Manager
WHERE managerid LIKE '%C\[\%]%' ESCAPE '\'
Here is some documentation that might be usefull to you: https://docs.microsoft.com/en-us/sql/t-sql/language-elements/like-transact-sql 以下是一些可能对您有用的文档: https : //docs.microsoft.com/zh-cn/sql/t-sql/language-elements/like-transact-sql
In case you are trying to searching with input 'C[%]' . 如果您尝试使用输入'C [%]'进行搜索。 Check this answer,
检查这个答案,
declare @str varchar(100)='C[%]'
SELECT m.*
FROM Manager m
WHERE REPLACE(m.managerid,'[','') LIKE '%'+@str+'%'
I got the solution. 我找到了解决方案。
declare @str varchar(100)='C\[\%]'
SELECT m.*
FROM Manager m
WHERE m.managerid LIKE '%'+@str+'%' ESCAPE '\'
It worked. 有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.