CASE
- Last Updated: September 13, 2023
- 2 minute read
- OpenEdge
- Version 12.2
- Documentation
Specifies a series of search conditions and
associated result expressions. The general form is called a searched
case expression. SQL returns the value specified by the first result
expression whose associated search condition evaluates as true.
If none of the search conditions evaluates as true, the CASE expression
returns a NULL value, or the value of some other
default expression if the CASE expression includes
the ELSE clause.
CASE also
supports syntax for a shorthand notation, called a simple case expression,
for evaluating whether one expression is equal to a series of other
expressions.
Syntax
|
Parameters
- searched_case_expr
-
Uses the following syntax:
CASE WHEN search_condition THEN {result_expr| NULL }[...] [ ELSE expr| NULL ] END - simple_case_expr
-
Uses the following syntax:
CASE primary_expr WHEN expr THEN {result_expr| NULL }[...] [ ELSE expr| NULL ] END - CASE
-
Specifies a searched case expression. It must be followed by one or more
WHEN-THENclauses, each specifying a search condition and corresponding expression. - WHEN search_condition THEN { result_expr| NULL }
-
Specifies a search condition and corresponding expression. SQL evaluates search_condition. If search_condition evaluates as true,
CASEreturns the value specified by result_expr, orNULL, if the clause specifiesTHEN NULL.If search_condition evaluates as false, SQL evaluates the next
WHEN-THENclause, if any, or theELSEclause, if it is specified. - CASE primary_expr
-
Specifies a simple case expression. In a simple case expression, one or more
WHEN-THENclauses specify two expressions. - WHEN expr THEN {result_expr| NULL }
-
Prompts SQL to evaluate expr and compare it with primary_expr specified in the
CASEclause. If they are equal,CASEreturns the value specified by result_expr (orNULL, if the clause specifiesTHEN NULL).If expr is not equal to primary_expr, SQL evaluates the next
WHEN-THENclause, if any, or theELSEclause, if it is specified. - ELSE {expr| NULL }
-
Specifies an optional expression whose value SQL returns if none of the conditions specified in
WHEN-THENclauses are satisfied. If theCASEexpression omits theELSEclause, it is the same as specifyingELSE NULL. - END
Specifies the end of case expression.
Notes
- This
function is not allowed in a
GROUP BYclause. - Arguments to this function cannot be query expressions.
Examples
A simple case expression can always be expressed as a searched case expression. This example illustrates a simple case expression:
|
The simple case expression in the preceding CASE example is equivalent to
the following searched case expression:
|
The following example shows a searched case expression that assigns a label denoting suppliers as 'In Mass' if the state column value is 'MA':
|
The following example shows the equivalent simple case expression:
|
Compatibility
SQL compatible