Skip to content
Advertisement

SQL Query rounding issue

I am using pyodbc to fetch total quantity of a product based on a certain criteria called “Strength”. The “Strength” column also has string value for some records, so it is a varchar column.

The user enters details like brand, product type, product line, date range, minimum quantity (in this case, 12), and strength range

This is my query:

This is my table:

This is the create statement

My problem is that this query results in a Quantity of 1200, even though it is outside the strength range. I am using SQL Server Management Studio V 18.4. How do I solve this?

Advertisement

Answer

In your WHERE clause you should use.

TRY_CAST([Strength] AS DECIMAL(10,4))>=0.2 AND TRY_CAST([Strength] AS DECIMAL(10,4))<=0.4

Because sql queries start working from where clauses( and joins) then executes other parts. SELECT is the least important part and if you only use CAST in your select it will be only useful for printing data as your preferred format.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement