This question already has answers here:
Rounding a number to the nearest 5 or 10 or X
(13 answers)
Closed 8 years ago.
I need to round down a number to nearest hundred like these:
For example:
54678 become 54600
54611 become 54600
54699 become 54600
Any idea? thanks!
Here you can find the code basics for the RoundDown command which allows you to round numbers up to a certain number of digits.
What you need to do is basically for example divide your number by 100, then round them and again multiply by 100. It's an easy workaround... but should work!
If you don't want 54699 to become 54700, but just want it to be 54600, there is an easy solution.
If the number is stored as an integer (no decimal point), just divide it by 100, then multiply it by 100 again
If it is not stored as an integer, you will need to divide it by 100, cast it to an integer and then multiply it by 100
Related
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.
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
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);
This question already has answers here:
SQL Server Strange Ceiling() behavior
(2 answers)
Closed 8 years ago.
I've found a really weird behavior in SQL Server 2012, the CEILING of 100 gives me 101 and sometimes 100.
I need to get the ceiling of a number considering 2 decimals, that means convert a 0.254 to 0.26
So I tried to run
SELECT CEILING(field * 100.0) / 100.0
FROM Table
This should work, and it does, at least for most of the data.
Any idea on how to solve it?
What you are seeing here is floating point errors. When you store a number in a floating point column, it isn't exact, so the number 1 may actually be 1.0000000000000000000000001. So multiplying it by 100 gives you a number a tiny bit greater than 100, hence CEILING rounds it up to 101.
The solution is to ROUND the number first which will remove the floating point errors. Note I have used 5 as the number of decimal places, you will need to decide on your own value of precision.
SELECT CEILING(ROUND(field,5)*100.0)/100.0 FROM Table
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