The data types varchar and varchar are incompatible in the modulo operator SQL Server

The data types varchar and varchar are incompatible in the modulo operator SQL Server



I get this error:



The data types varchar and varchar are incompatible in the modulo operator.



when running this code


' Number LIKE '%' + @search + '%' ';



The number is an integer and this code snippet appears enclosed in ''; due to a dynamic pivot statement. Any ideas how i can run this? thanks





Please show a more complete SQL statement.
– Gordon Linoff
Aug 28 at 10:55





If your percent sign, meant to be around your search parameter, i.e. WHERE Foo LIKE '%bar%' and you get a modulo operator error, you have a quote mismatch somewhere. Read How to Ask and show a Minimal, Complete, and Verifiable example.
– CodeCaster
Aug 28 at 10:56


WHERE Foo LIKE '%bar%'





Could you use an IDE that color codes string literals so that you could see the problem yourself?
– Salman A
Aug 28 at 11:00




3 Answers
3



Presumably, you have something like this:


SELECT ' Number LIKE '%' + @search + '%' ';



If so, you have single quotes inside a string. So the "string" is interpreted as:


SELECT (' Number LIKE ') % (' + @search + ') % (' ');



The % is the modulo operator . . . and hence your error.


%



In order to put single quotes in a string, you need to double them up. Something like this:


SELECT ' Number LIKE ''%'' + @search + ''%'' ';



Are you sure you need those outer quotes, should it not just be


Number LIKE '%' + @search + '%'



Otherwise you'll need to escape the inner quotes. Try this:


'Number LIKE ''%'' + @search + ''%'''



Because % is a modulo operator in SQl Server and it is interpreted as such because to insert quto in a string you need to double it as other answers suggest.


%



Other solution would be:


'CHARINDEX(' + @search + ', Number) > 0'



I always tend to use that over LIKE oprator with %someString%. Both determine if there is string within the other.


LIKE


%someString%






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)