Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Simple arithmetic (subtraction) wrong result?

Author  Topic 

eagle1fox2
Starting Member

3 Posts

Posted - 2012-11-06 : 12:11:58
Hi all,

This seems to be a simple arithmetic problem, but it has me baffled. Does anyone know why the following SELECT does not return a result of 5? Since it returns 15 instead, it´s as if though I had used plus signs instead of minuses! I am using SQL Server 2008 R2.

SELECT
(
10 - (0 - 5)
)



I would've thought SQL first tries to subtract 5 from 0, thus resulting in -5, then subtracting that from 10, resulting in 5. But somehow, it went ahead and just added up everything, despite the minus signs. That is really strange. Does anyone know why it's doing this? If I use this:

SELECT 
(
(10 - 0 - 5)
)


Then it DOES work as expected. But don't know why the first select doesn't...

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-06 : 12:44:24
its returning expected result
in first case it calculates 0-5 first because of () so result is -5

then it subtracts it from 10

so 10-(-5) which is 10 + 5 = 15

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -