SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

eagle1fox2
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.

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

India
52325 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
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.05 seconds. Powered By: Snitz Forums 2000