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

In summary, the conversation is about trying to find all the even numbers in a specific field in SQL Server. The person is using SQL Zoo and is unable to get the correct results, possibly due to a bug in the server or their computer. They are trying to avoid using T-SQL and variables and have been suggested to try using the mod() function in Mysql instead of the % operator. They are also advised to try typing the query without %2 to see if they get the same results.
  • #1
WWGD
Science Advisor
Gold Member
7,010
10,469
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
  • #3
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
  • #4
Thanks, Jedi, the strange thing is that the query actually runs , but returns incorrect results.
 
  • #5
Could it be evaluating the query as?
SQL:
SELECT [FieldName] FROM Table WHERE [FieldName]  =0 ;

where the %2 has been removed.
 
  • #6
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:
  • #7
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.
 
  • #8
Try typing in the query without the %2 and see if you get the same result set.
 

1. What is the purpose of this SQL Server query?

The purpose of this query is to retrieve a list of even numbers from a specified data column in a SQL Server database.

2. How do I specify the data column I want to retrieve even numbers from?

You can specify the data column by replacing the "column_name" placeholder in the query with the name of the column you want to retrieve even numbers from.

3. Can I use this query on any data type?

Yes, this query can be used on any data type that can store numbers, such as integer, decimal, or float.

4. Will this query return duplicate even numbers?

No, this query uses the DISTINCT keyword to ensure that only unique even numbers are returned.

5. Can I modify this query to retrieve odd numbers instead?

Yes, you can modify this query by changing the WHERE condition to specify odd numbers instead of even numbers. For example, you can change "WHERE column_name % 2 = 0" to "WHERE column_name % 2 = 1" to retrieve odd numbers.

Similar threads

  • Programming and Computer Science
2
Replies
51
Views
4K
  • Programming and Computer Science
Replies
7
Views
436
  • Programming and Computer Science
Replies
18
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
Replies
11
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Computing and Technology
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
5K
Back
Top