Expressions are part of PL/SQL that evaluates to something, but is not valid on its own. For example, an expression can appear on the right hand side of an assignment operator, but that expression can not be used on its own, it has to e part of another statement. For example, 2 + 3 * 3 is an expression, but can not be executed as it is.
Evaluating expressions
The precedence of the operator determines the order of evaluation. For example Multiplication has a higher precedence than addition, thus the expression above as it is will be evaluates as 2 + 9 = 11. But if we needed to give the addition of 2 and 3, precedence before multiplying their result by 3; we will have to put those two in parenthesis or brackets. Thus, we will have (2 + 3) * 3 = 15.
Types of PL/SQL expressions
Boolean Expressions - these are the kinds of expressions that return either TRUE or FALSE. For example a > b, (2 < 4) OR (-2 > c), NULL are all valid Boolean expressions. The way it works is that you take one of the three Boolean operators (AND, OR, NOT), these operators take one or more arguments and return either TRUE or FALSE. Here is a small table of how Boolean operators work :
NOT |TRUE |FALSE|NULL
-------------------------
|FALSE|TRUE |NULL
This means that the not operator always return the opposite of the argument supplied, except when that argument is NULL, then the answer will still be NULL. Now let us take a look at how the AND operator works:
AND |TRUE |FALSE|NULL
-------------------------
TRUE |TRUE |FALSE |NULL
FALSE |FALSE|FALSE |FALSE
NULL |NULL |FALSE |NULL
This means, the AND operator only returns TRUE when both arguments are TRUE. It returns FALSE when one of the arguments evaluates to FALSE and then Returns null when at least one of the arguments evaluates to NULL and the other argument does not evaluate to FALSE. Here is what happens when an OR operator is applied:
OR |TRUE |FALSE|NULL
-------------------------
TRUE |TRUE |TRUE |TRUE
FALSE |TRUE |FALSE|NULL
NULL |TRUE |NULL |NULL
As you can see from the table above, with the OR Operator, you only need one argument to evaluate to TRUE for the whole expression to result in a TRUE. And a FALSE with a NULL always result in a NULL.
character expressions
The only character operator in PL/SQL is the concatenation (||). This is used to merge two strings or characters into one string. For example, 'My name' || 'is Mamamia' evaluates to 'My name is Mamamia'.
Comparison operators
This group of operators include the equality(=), inequality (!=), less than (<), less than or equal to (<=), greater than (>) and greater than or equal to (>=). For these operators, they can only return NULL if their operant is NULL.
LIKE operator is used to match patterns in character strings. It makes use of characters and symbols such as (%) to denote a match of zero or more characters. The underscore ( _ ) is used to match exactly one character. You can also take a look at SQL BETWEEN and IN operators
|