Search special characters from SQL table

2019-08-02 14:53发布

问题:

Text = “World“ world ” <world <Word> ‘ word’ ‘ word“ word’ =1254.25 = 2545.58. 20%

Hey guys,

I need to search a word from a string which was saved in my db. The searching word contains special characters such as ""'!@#$%^<>. Below is the sql select query used for the search.

select * from TABLE  where TABLE.text like ('%'+ '“ World' +'%')

as a result some of the special character are not searched. Characters such as double quotation, single quotation are not searched from this. need assistance with solving this problem asap. thank you :)

回答1:

These Special characters are Unicode characters, When ever dealing with these characters in sql server you have to tell sql server explicitly that there can be some unicode characters in the strings you are about to manipulate by prefixing your strings with N'String'

In your case you would write a query something like ....

select * from TABLE  
where TABLE.text like N'%'+ N'“ World' + N'%'


回答2:

you can use the ESCAPE clause and escape your parameter value. http://technet.microsoft.com/en-us/library/ms179859.aspx

so for example to search for a string containing the character %

select * from TABLE  where TABLE.text like ('%'+ N'\%' +'%') ESCAPE '\'