SQL Math Order of Operations [duplicate] - sql

This question already has answers here:
SQL Server, division returns zero
(6 answers)
Closed 1 year ago.
Why doesn't this produce 50 when I use this select statement in SSMS? Instead, it returns '0'.
select ((1500-1000)/1000)*100

Because SQL Server does integer division. So 1500 - 1000 is 500. And 500 / 1000 is 0, not 0.5.
You can introduce a decimal point for more precision:
((1500-1000)/1000.0)*100

Related

SQL difference between dividing by float and by int [duplicate]

This question already has answers here:
How to get a float result by dividing two integer values using T-SQL?
(10 answers)
SQL Server, division returns zero
(6 answers)
Closed 8 months ago.
so I'm currently learning SQL and one of the exercies was to:
Show patient_id, weight, height, isObese from the patients table. Display isObese as a boolean 0 or 1. Obese is defined as weight(kg)/(height(m)2) >= 30. Weight is in units kg and height in units cm.
I managed to do it this way:
SELECT patient_id, weight, height,
CASE
WHEN weight/POWER(height/100,2) >= 30 then '1'
ELSE '0' END AS isObese
FROM patients
and it was incorrect. The correct solution was to write: weight/POWER(height/100.0,2).
So my question is why I have to divide by 100.0 instead of 100 to change from centimeters to meters? What is the difference? In this table every height value was an int type, without decimal numbers.

Rounding hour numbers doing daylight saving time [duplicate]

This question already has answers here:
How to get a float result by dividing two integer values using T-SQL?
(10 answers)
Can`t divide two INTEGERS in SQL Server [duplicate]
(2 answers)
SQL Server, division returns zero
(6 answers)
Closed 11 months ago.
Can anyone show how I do something as simple as round to the nearest integer?
select round((24+24+24)/24,0) result: 3
select round((24+24+23)/24,0) result: 2
select round((24+24+25)/24,0) result: 3
Is there something I can do so all three examples will give me the result 3?
Thank you in advance
The problem is integer division. All of the values are integers, so the 2.9whatever is truncated to just 2 before you even start rounding.
This works just fine by including a floating point value with the division operation:
select round((24+24+24)/24.0,0)
select round((24+24+23)/24.0,0)
select round((24+24+25)/24.0,0)
The other option is always just throw in a bonus hour. Then you don't even need to round:
select (24+24+24+1)/24
select (24+24+23+1)/24
select (24+24+25+1)/24

Can`t divide two INTEGERS in SQL Server [duplicate]

This question already has answers here:
Integer division in sql server
(8 answers)
Closed 1 year ago.
Why do I get 3 when I execute this T-SQL script:
SELECT 10/3;
I have tried :
SELECT CAST((10/3) AS decimal(5,2));
But now I get 3.00 - still not what I expected
If you are using two integers in a division it will do integer division. You need to cast at least one of the integers to float or decimal before the division to get what you are looking for.
{int}/{int}={int}. SQL Server, or any good language fior that matter, won't implicitly change the data type of a expressions results when only 1 data type is involved (in this case int). Thus 10/3 = 3 is correct.
As for the latter, you still have {int}/{int}={int}, but you then convert the result to a decimal. 3 as a decimal is 3.00 so that too is correct.
You need to convert one of values before you divide; changing the data type of either the dividend or the divisor works. For example:
SELECT (10 * 1.) / 3,
10 / CONVERT(decimal(2,0),3);

i would like to display only non rounded decimal value in sql access [duplicate]

This question already has answers here:
Difference between numeric, float and decimal in SQL Server
(8 answers)
Closed 5 years ago.
I wanted to to display only the non rounded decimal values using sql access.
I have tried the the query;
select * from table1 where oc_amount!=0;
But its not returning the value.
enter image description here
You would need to compare the raw value to the rounded value
select * from table1 where oc_amount <> Round(oc_amount, 0)
If that isn't what you really wanted, try:
select * from table1 where (Round(oc_amount, 2) - Round(oc_amount, 0)) > 0
(*adjust the number of decimals of rounding to suit your need)

SQL - Rounding off numbers [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Accuracy in rounding numbers
I have the following requirement-
Get the A_MINUTES column value from TableA for all rows
Sum the A_MINUTES.
Convert the summed minutes values to hours - divide by 60
Round off the final hours value to 2 decimal places.
This needs to be written in SQL. Do you think the following query will have any rounding errors?
SELECT ROUND ( (SUM(A_MINUTES)/60.0) , 2) FROM TABLEA
If point 3.Sum the hours values to be considered you are missing one SUM function.
SELECT ROUND ( SUM(SUM(A_MINUTES)/60.0) , 2) FROM TABLEA