MrPink
MrPink

Reputation: 1371

SQL Server 2008 Calculation Bug

Here is the query i am running

Select (100/18)*18

The answer i get is 90. I have tried declaring each of the numbers as decimals also, and i get the value 100.0008, but when i calculate with variables as floats the answer is correct at '100' so does anyone have any idea why SQL calculates like this?

Upvotes: 0

Views: 106

Answers (4)

Joe G Joseph
Joe G Joseph

Reputation: 24046

to get the desired result you should try something like this:

Select CEILING((100/18.0)*18)

when you do this,

Select (100/18)*18

sql server consider operation as integer division and take select 100/18 as 5 instaed of 5.55555....

Upvotes: 2

digscoop
digscoop

Reputation: 371

Try to express the numders ac decimal numbers

SELECT (100.0/18)*18

and

SELECT ROUND((100.0/18)*18,2)

...

Upvotes: 1

SRIRAM
SRIRAM

Reputation: 1888

you can try this

select abs(18*100/18)

Upvotes: 0

Yograj Gupta
Yograj Gupta

Reputation: 9869

Because it will first evelute the paranthesis

So

Select (100/18)*18

will (100/18) = 5.555555 but it assume both number as int so it will cast the result as int so it will return 5

so now 5*18 = 90

If you want the correct result do this instead Select (100*18)/18

Upvotes: 7

Related Questions