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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Simple arithmetic (subtraction) wrong result?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

3 Posts

Posted - 11/06/2012 :  12:11:58  Show Profile  Reply with Quote
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.

    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:

	(10 - 0 - 5)

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

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 11/06/2012 :  12:44:24  Show Profile  Reply with Quote
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

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000