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);
Related
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:
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
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)
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