SQL Server throwing error in SQL for Odd / Even check
NickName:codacopia Ask DateTime:2021-01-27T00:55:33

SQL Server throwing error in SQL for Odd / Even check

I have a SQL Server database that is throwing an error on one of my tables when I add a check for an even or odd number value.

Here is an example script which would cause the error:

SELECT TOP 5 * 
FROM [TABLE_QUALIFIER].[TABLE_OWNER].[TABLE_NAME] WITH (nolock) 
WHERE street_number BETWEEN '1' AND '5000' 
  AND (street_number % 2) > 0 
  AND street_name LIKE '%Main St%' 
ORDER BY CAST(street_number AS float);

As you can see, I have this piece of the statement checking for odd results:

AND (street_number % 2) > 0

If I eliminate this bit of the call, I get a successful query result. However, in this case I need to include the check of odd street number results.

When I check the table info with this command:

exec sp_columns [TABLE_NAME];

I can see that the street_number column is saved as a varchar type. In the SQL statement, you can see that I am casting the street_number value as a float.

Any suggestions on how I can amend this SQL query to remove the error when checking for an odd street_number?

Additional info:

Microsoft SQL Server 2017 (RTM-CU19) (KB4535007) - 14.0.3281.6 (X64) Jan 23 2020 21:00:04 Copyright (C) 2017 Microsoft Corporation Standard Edition (64-bit) on Windows Server 2016 Datacenter 10.0 (Build 14393: ) (Hypervisor)

Copyright Notice:Content Author:「codacopia」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/65905672/sql-server-throwing-error-in-sql-for-odd-even-check

More about “SQL Server throwing error in SQL for Odd / Even check” related questions

SQL Server throwing error in SQL for Odd / Even check

I have a SQL Server database that is throwing an error on one of my tables when I add a check for an even or odd number value. Here is an example script which would cause the error: SELECT TOP 5 * ...

Show Detail

AddToRoleAsync is throwing strange SQL error

In my ASP.NET MVC Core 1.1 app, the AddToRoleAsync call in the last line of the following code is throwing the error: The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION. This is ...

Show Detail

how to show only even or odd rows in sql server 2008?

i have a table MEN in sql server 2008 that contain 150 rows. how i can show only the even or only the odd rows ?

Show Detail

Simple query throwing an SQL syntax error

I've got a simple SQL query that keeps throwing an SQL syntax error. It looks like this: $sql = "SELECT COUNT(*) AS TOTAL FROM PRODUCT WHERE ID_PRODUCT = ".$id; $result = mysql_query($sql) or die(

Show Detail

SQL Server sorting odd numbers ASC and even numbers DESC

I am wondering if there is a way in SQL Server to sort a table which holds numbers in a varchar column. I went and cast the number (in my case House Numbers) to Int and use order by cast([sano]%2 ...

Show Detail

Even or Odd String Check

I'm trying to write a program that checks whether the input string is even or odd and I keep getting errors I can't figure out why. (i = odd, ii = even, iii = odd, iiii = even) and so on. I tr...

Show Detail

SQL Server: How to make server check all its check constraints?

It seems that some scripts generated by Enterprise Manager* (or not, it doesn't matter) created check constraints WITH NOCHECK. Now when anyone modifies the table, SQL Server is stumbling across fa...

Show Detail

Sqlcmd error while connecting with SQL Server instance on a VM using SQL Server proxy

I have created a private SQL Server instance and trying to access it on Virtual Machine instance using SQL Server proxy using the link (https://cloud.google.com/sql/docs/sqlserver/connect-instance-

Show Detail

PDO keeps throwing error on email check?

if(!empty($_POST['email'])) { $params = array( ':email' => $email ); $sql = "SELECT `email` FROM `tablename` WHERE email = :email"; } else return false; $stmt = $user->query

Show Detail

MySql procedure IF number is odd or even

My learning mysql and my question is to check what i'm doing wrong here: I'm trying to make a sql procedure to check if the first number of a number user enters is odd or even but im getting syntax

Show Detail