Operator Precedence (Transact-SQL)
When a complex expression has multiple operators, operator precedence determines the sequence of operations. The order of execution can significantly affect the resulting value.
Operators have the precedence levels shown in the following table. An operator on higher levels is evaluated before an operator on a lower level. In the following table, 1 is the highest level and 8 is the lowest level.
|1||~ (Bitwise NOT)|
|2||* (Multiplication), / (Division), % (Modulus)|
|3||+ (Positive), - (Negative), + (Addition), + (Concatenation), - (Subtraction), & (Bitwise AND), ^ (Bitwise Exclusive OR), | (Bitwise OR)|
|4||=, >, <, >=, <=, <>, !=, !>, !< (Comparison operators)|
|7||ALL, ANY, BETWEEN, IN, LIKE, OR, SOME|
When two operators in an expression have the same precedence level, they're evaluated left to right based on their position in the expression. For example, in the expression that is used in the following
SET statement, the subtraction operator is evaluated before the addition operator.
DECLARE @MyNumber int; SET @MyNumber = 4 - 2 + 27; -- Evaluates to 2 + 27 which yields an expression result of 29. SELECT @MyNumber;
Use parentheses to override the defined precedence of the operators in an expression. Everything within parentheses is evaluated to yield a single value. That value can be used by any operator outside those parentheses.
For example, in the expression used in the following
SET statement, the multiplication operator has a higher precedence than the addition operator. The multiplication operation is evaluated first; the expression result is
DECLARE @MyNumber int; SET @MyNumber = 2 * 4 + 5; -- Evaluates to 8 + 5 which yields an expression result of 13. SELECT @MyNumber;
In the expression used in the following
SET statement, the parentheses cause the addition to be evaluated first. The expression result is
DECLARE @MyNumber int; SET @MyNumber = 2 * (4 + 5); -- Evaluates to 2 * 9 which yields an expression result of 18. SELECT @MyNumber;
If an expression has nested parentheses, the most deeply nested expression is evaluated first. The following example contains nested parentheses, with the expression
5 - 3 in the most deeply nested set of parentheses. This expression yields a value of
2. Then, the addition operator (
+) adds this result to
4 which yields a value of
6. Finally, the
6 is multiplied by
2 to yield an expression result of
DECLARE @MyNumber int; SET @MyNumber = 2 * (4 + (5 - 3) ); -- Evaluates to 2 * (4 + 2) which then evaluates to 2 * 6, and -- yields an expression result of 12. SELECT @MyNumber;