Search special characters from SQL table

放肆的年华 提交于 2019-12-11 12:15:10

问题


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 '\'


来源:https://stackoverflow.com/questions/23327410/search-special-characters-from-sql-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!