Need to write a SQL query to search special character in a column. Text needed to search is ‘R&D’ but issue is that SQL server 2005 is taking it as a logical operator.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I guess you are using parameters, correct?
Because if you do NOT use parameters, it works (see the other answers).
What doesn’t work is this:
The only way that I found to make it work with parameters is doing it like this:
EDIT:
First of all, I need to know if you’re using parameters or not.
If not, I don’t understand the problem at all.
One of these will work (with or without LIKE):
If you do work with parameters, my above solution ( … like ‘%’ + @tmp + ‘%’) is the only one that I know of.
EDIT 2
Now that I know that the data type is “text”:
The problem is that you can only search text with LIKE (not with ‘=’) and you can’t index a text column to increase speed.
The text data type is deprecated, so you should change it into varchar(max) anyway.
And varchar(max) doesn’t have the limitations that text has: you can search it with ‘=’ and you can index it.
So that would be the correct solution: change it from text to varchar(max) and everything will work!