A strange operation problem in SQL Server (-100/-100*10 = 0)

  • A+
Category:Languages
  • If you execute SELECT -100/-100*10 the result is 0.
  • If you execute SELECT (-100/-100)*10 the result is 10.
  • If you execute SELECT -100/(-100*10) the result is 0.
  • If you execute SELECT 100/100*10 the result is 10.

BOL states:

When two operators in an expression have the same operator precedence level, they are evaluated left to right based on their position in the expression.

And

Level   Operators   1     ~ (Bitwise NOT)   2     * (Multiplication), / (Division), % (Modulus)   3     + (Positive), - (Negative), + (Addition), + (Concatenation), - (Subtraction), & (Bitwise AND), ^ (Bitwise Exclusive OR), | (Bitwise OR) 

Is BOL wrong, or am I missing something? It seems the - is throwing the (expected) precedence off.

 


According to the precedence table, this is the expected behavior:

-100 / -100 * 10 

Is evaluated as:

-100 / -(100 * 10) 

Note that this behavior is different from most programming languages where unary operators have higher precedence than multiplication and division e.g. VB, JavaScript.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: