Embarrassingly Simple SQL Server query: List Even numbers in....

AI Thread Summary
The discussion revolves around an issue with querying even numbers in SQL Server using the modulus operator. The user attempts to execute the query "SELECT [FieldName] FROM Table WHERE [FieldName] % 2 = 0" but encounters an error message. They mention using SQL Zoo and express a preference for avoiding T-SQL and variables. Suggestions include using parentheses around the modulus operation to address potential precedence issues and considering the possibility that the '%' character might be misinterpreted in certain contexts, such as Windows scripts. Despite the query running, the user notes it returns incorrect results, prompting speculation about evaluation errors or potential bugs in the server or client. The conversation emphasizes troubleshooting steps and alternative approaches, such as using the mod() function instead of the '%' operator.
WWGD
Science Advisor
Homework Helper
Messages
7,700
Reaction score
12,732
Hi,
I am trying to find all the even numbers in [FieldName] in SQL Server.

My query is : SELECT [FieldName] FROM Table WHERE [FieldName] % 2 =0 ;

I only get an error message . ( I am using SQL Zoo, since I don't have SQL server available at the moment.

Only message I get is that the query is incorrect. I am trying to avoid using T-Sql, variables, etc.
 
Technology news on Phys.org
One other thought is the % character is used to identify script parameters on windows (%1, %2, %3 ...) so if your sql is in such a script then that could be why its not working and why the mod() function would be a better choice.

Here's some writeup for Windows-XP which is probably still somewhat valid today:

https://www.microsoft.com/resources...docs/en-us/ntcmds_shelloverview.mspx?mfr=true
 
Last edited:
  • Like
Likes WWGD
Thanks, Jedi, the strange thing is that the query actually runs , but returns incorrect results.
 
Could it be evaluating the query as?
SQL:
SELECT [FieldName] FROM Table WHERE [FieldName]  =0 ;

where the %2 has been removed.
 
jedishrfu said:
Could it be evaluating the query as?
SQL:
SELECT [FieldName] FROM Table WHERE [FieldName]  =0 ;

where the %2 has been removed.
I don't think so, since [FieldName] is the primary key. There may be a bug in the server or the (client) computer.
 
Last edited:
WWGD said:
I don't think so, since [FieldName] is the primary key. There may be a bug in the server or the (client) computer.
Thanks for the effort, though, a tough one.
 
Try typing in the query without the %2 and see if you get the same result set.
 
Back
Top