You are on page 1of 8

Expressions and Comparisons

Expressions and Comparisons


Expressions are constructed using operands and operators Operand=variable, constant, literal Operators = +,- etc Operator Precedence
Operator **, NOT +, *, / +, -, || =, <, >, <=, >=, <>, !=, ~=, ^=, IS NULL, LIKE, BETWEEN, IN AND OR Operation exponentiation, logical negation identity, negation multiplication, division addition, subtraction, concatenation comparison conjunction inclusion

12 / 4 + 5, (8 + 6) / 2, 100 + (20 / 5 + (7 - 3))

Logical Operators
The logical operators AND, OR, and NOT follow the tri-state logic shown in Table AND and OR are binary operators; NOT is a unary operator.

x
TRUE TRUE TRUE FALSE FALSE FALSE NULL NULL NULL

y
TRUE FALSE NULL TRUE FALSE NULL TRUE FALSE NULL

x AND y
TRUE FALSE NULL FALSE FALSE FALSE NULL FALSE NULL

x OR y
TRUE TRUE TRUE TRUE FALSE NULL TRUE NULL NULL

NOT Y
FALSE TRUE NULL

Short-Circuit Evaluation DECLARE ... on_hand INTEGER; on_order INTEGER; BEGIN .. IF (on_hand = 0) OR ((on_order / on_hand) < 5) THEN END IF; END;

...

When the value of on_hand is zero, the left operand yields TRUE, so PL/SQL need not evaluate the right operand. If PL/SQL were to evaluate both operands before applying the OR operator, the right operand would cause a division by zero error. In any case, it is a poor programming practice to rely on short-circuit evaluation.

Comparison Operators
Comparison operators compare one expression to another. The result is always true, false, or null. Typically, you use comparison operators in conditional control statements and in the WHERE clause of SQL data manipulation statements. Here are a couple of examples:

IF quantity_on_hand > 0 THEN UPDATE inventory SET quantity = quantity - 1

WHERE part_number = item_number; ELSE ... END IF;

Company Logo

Relational Operators
Operator
= <>, !=, ~=, ^= < > <= >=

www.desinfreebies.org

Meaning equal to not equal to less than greater than less than or equal to greater than or equal to

IS NULL Operator IF variable = NULL THEN ... Instead, use the following statement: IF variable IS NULL THEN ...

LIKE Operator ename LIKE 'J%SON(wildcards. An underscore (_) matches exactly one character; a percent sign (%)) BETWEEN Operator 45 BETWEEN 38 AND 44 IN Operator DELETE FROM emp WHERE ename IN (NULL, 'KING', 'FORD'); Concatenation Operator 'suit' || 'case Boolean Expressions TRUE, FALSE, or NULL. Arithmetic Expressions number1:= 75;number2:= 70; the following expression is true: number1 > number2

www.designfreebies.org

Character Expressions string1:= 'Kathy';string2:= 'Kathleen'; the following expression is true: string1 > string2 Date Expressions date1:= '01-JAN-91';date2:= '31-DEC-90'; the following expression is true: date1 > date2

Note: 100 < tax < 500 -- illegal The debugged version follows: (100 < tax) AND (tax < 500) Boolean variable WHILE NOT (done = TRUE) LOOP ...END LOOP; can be simplified as follows: WHILE NOT done LOOP ...END LOOP;

www.designfreebies.org

Thank You !

You might also like