Structure of Arithmetic Expressions
Order of Evaluation
The following table lists the precedence of operations in arithmetic expressions.
Precedence |
Operation |
Highest |
+ operand (unary plus) - operand (unary minus) |
Intermediate |
operand ** operand (exponentiation) |
operand * operand (multiplication) operand / operand (division) operand MOD operand (modulo operator) |
|
operand + operand (addition) operand - operand (subtraction) |
In general, the order of evaluation is:
1 Operations enclosed in parentheses are performed first.
2 When no parentheses are present, operations are performed in order of precedence.
3 Operators of the same precedence are evaluated from left to right.
The Optimizer may reorder evaluations based on associative and commutative properties of the operations involved.
Format
The format of an arithmetic expression is the same as the default format of the result data type.
You can use the FORMAT phrase to change the default format of the result data type. The FORMAT phrase is relevant only in field mode, such as BTEQ applications, and in conversion to a character data type.
Example
You want to raise the salary for each employee in department 600 by $200 for each year spent with the company (up to a maximum of $2500 per month).
To determine who is eligible, and the new salary, enter the following statement:
SELECT Name, (Salary+(YrsExp*200))/12 AS Projection
FROM Employee
WHERE Deptno = 600
AND Projection < 2500 ;
This statement returns the following response:
Name Projection
-------- ----------
Newman P 2483.33
The statement uses parentheses to perform the operation YrsExp * 200 first. Its result is then added to Salary and the total is divided by 12.
The parentheses enclosing YrsExp * 200 are not strictly necessary, but the parentheses enclosing Salary + (YrsExp * 200) are necessary, because, if no parentheses were used in this expression, the operation YrsExp * 200 would be divided by 12 and the result added to Salary, producing an erroneous value.
The phrase AS Projection in this example associates the arithmetic expression (Salary + (YrsExp * 200)/12) with Projection. Using the AS phrase lets you use the name Projection in the WHERE clause to refer to the entire expression.
The result is formatted without a comma separating thousands from hundreds.