9.17. Conditional Expressions
This section describes theSQL-compliant conditional expressions available inPostgres Pro. If your needs go beyond the capabilities of these conditional expressions, you might want to consider writing a stored procedure in a more expressive programming language. TheSQL An example: The data types of all the There is a“simple” form of The first The example above can be written using the simple A As described inSection 4.2.14, there are various situations in which subexpressions of an expression are evaluated at different times, so that the principle that“ The This returns Like a The In this example, if The Note thatTip
9.17.1.
CASE
CASE
expression is a generic conditional expression, similar to if/else statements in other programming languages:CASE WHEN
condition
THENresult
[WHEN ...] [ELSEresult
]ENDCASE
clauses can be used wherever an expression is valid. Eachcondition
is an expression that returns aboolean
result. If the condition's result is true, the value of theCASE
expression is theresult
that follows the condition, and the remainder of theCASE
expression is not processed. If the condition's result is not true, any subsequentWHEN
clauses are examined in the same manner. If noWHEN
condition
yields true, the value of theCASE
expression is theresult
of theELSE
clause. If theELSE
clause is omitted and no condition is true, the result is null.SELECT * FROM test; a--- 1 2 3SELECT a, CASE WHEN a=1 THEN 'one' WHEN a=2 THEN 'two' ELSE 'other' END FROM test; a | case---+------- 1 | one 2 | two 3 | other
result
expressions must be convertible to a single output type. SeeSection 10.5 for more details.CASE
expression that is a variant of the general form above:CASE
expression
WHENvalue
THENresult
[WHEN ...] [ELSEresult
]ENDexpression
is computed, then compared to each of thevalue
expressions in theWHEN
clauses until one is found that is equal to it. If no match is found, theresult
of theELSE
clause (or a null value) is returned. This is similar to theswitch
statement in C.CASE
syntax:SELECT a, CASE a WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'other' END FROM test; a | case---+------- 1 | one 2 | two 3 | other
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:SELECT ... WHERE CASE WHEN x <> 0 THEN y/x > 1.5 ELSE false END;
Note
CASE
evaluates only necessary subexpressions” is not ironclad. For example a constant1/0
subexpression will usually result in a division-by-zero failure at planning time, even if it's within aCASE
arm that would never be entered at run time.9.17.2.
COALESCE
COALESCE
(value
[, ...])COALESCE
function returns the first of its arguments that is not null. Null is returned only if all arguments are null. It is often used to substitute a default value for null values when data is retrieved for display, for example:SELECT COALESCE(description, short_description, '(none)') ...
description
if it is not null, otherwiseshort_description
if it is not null, otherwise(none)
.CASE
expression,COALESCE
only evaluates the arguments that are 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 toNVL
andIFNULL
, which are used in some other database systems.9.17.3.
NULLIF
NULLIF
(value1
,value2
)NULLIF
function returns a null value ifvalue1
equalsvalue2
; otherwise it returnsvalue1
. This can be used to perform the inverse operation of theCOALESCE
example given above:SELECT NULLIF(value, '(none)') ...
value
is(none)
, null is returned, otherwise the value ofvalue
is returned.9.17.4.
GREATEST
andLEAST
GREATEST
(value
[, ...])LEAST
(value
[, ...])GREATEST
andLEAST
functions select the largest or smallest value from a list of any number of expressions. The expressions must all be convertible to a common data type, which will be the type of the result (seeSection 10.5 for details). NULL values in the list are ignored. The result will be NULL only if all the expressions evaluate to NULL.GREATEST
andLEAST
are not in the SQL standard, but are a common extension. Some other databases make them return NULL if any argument is NULL, rather than only when all are NULL.