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

Click For 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,771
Reaction score
12,990
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
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.
 

Similar threads

  • · Replies 51 ·
2
Replies
51
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
11
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K