Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

Tags:
  1. May 2, 2017 #1

    WWGD

    User Avatar
    Science Advisor
    Gold Member

    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.
     
  2. jcsd
  3. May 2, 2017 #2

    jedishrfu

    Staff: Mentor

  4. May 3, 2017 #3

    jedishrfu

    Staff: Mentor

    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: May 3, 2017
  5. May 3, 2017 #4

    WWGD

    User Avatar
    Science Advisor
    Gold Member

    Thanks, Jedi, the strange thing is that the query actually runs , but returns incorrect results.
     
  6. May 3, 2017 #5

    jedishrfu

    Staff: Mentor

    Could it be evaluating the query as?
    Code (SQL):

    SELECT [FieldName] FROM TABLE WHERE [FieldName]  =0 ;
     
    where the %2 has been removed.
     
  7. May 3, 2017 #6

    WWGD

    User Avatar
    Science Advisor
    Gold Member

    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: May 3, 2017
  8. May 3, 2017 #7

    WWGD

    User Avatar
    Science Advisor
    Gold Member

    Thanks for the effort, though, a tough one.
     
  9. May 3, 2017 #8

    jedishrfu

    Staff: Mentor

    Try typing in the query without the %2 and see if you get the same result set.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Embarrassingly Simple SQL Server query: List Even numbers in...
  1. I need a SQL query (Replies: 3)

  2. Need Help on SQL query (Replies: 3)

  3. Basic SQL queries (Replies: 6)

Loading...