The SQL CASE expression is a generic conditional expression, similar to if/else statements in other languages:
CASE clauses can be used wherever an expression is valid. condition is an expression that returns a BOOLEAN result. If the result is true then the value of the CASE expression is the result that follows the condition. If the result is false any subsequent WHEN clauses are searched in the same manner. If no WHEN condition is true then the value of the CASE expression is the result in the ELSE clause. If the ELSE
clause is omitted and no condition matches, the result is null
The expression is computed and compared to all the value specifications in the WHEN clauses until one is found that is equal. If no match is found, the result in the ELSE
clause (or a null value) is returned.
A CASE expression does not evaluate any subexpressions that are not needed to determine the result. For example, this is a possible way of avoiding a division-by-zero failure:
The COALESCE function returns the first of its arguments that is not null. Null is returned only if all arguments are null.
Like a CASE expression, COALESCE will not evaluate arguments that are not needed to determine the result; that is, arguments to the right of the first non-null argument are not evaluated. This SQL-standard function provides capabilities similar to NVL and IFNULL, which are used in some other database systems.
The NULLIF function returns a null value if value1 and value2 are equal; otherwise it returns value1.
If value1 is (none), return a null, otherwise return value1.
The NVL function returns the first of its arguments that is not null. NVL
evaluates the first expression; if that expression evaluates to NULL
returns the second expression.
evaluates an expression, and returns either the second or third expression, depending on the value of the first expression. If the first expression is not NULL
returns the value in expr2
; if the first expression is NULL
returns the value in expr3
The GREATEST and LEAST functions select the largest or smallest value from a list of any number of expressions.
Note that GREATEST
are not in the SQL
standard, but are a common extension.