This document describes two types of conditional expressions in SQL called CASE statements: CASE (Switch) and CASE (If).
CASE (Switch) evaluates an expression and assigns a value based on strict equality matches to WHEN clauses, defaulting to an ELSE value. CASE (If) evaluates conditions in WHEN clauses using comparison operators, assigning values accordingly and defaulting to an ELSE value if no matches. Both provide syntax examples to demonstrate their usage.
1 of 3
Download to read offline
More Related Content
Conditional expressions
1. Conditional Expressions
Conditional Expressions
Expressions are building blocks for creating conditional expressions that convert a value from one form to
another. Expressions include:
CASE (Switch) CASE (If)
CASE (Switch)
This form of the CASE statement is also referred to as the CASE(Lookup) form. The value of expr1 is
examined, then the WHEN expressions. If expr1matches any WHEN expression, it assigns the value in the
corresponding THEN expression.
If none of the WHEN expressions match, it assigns the default value specified in the ELSE expression. If
no ELSE expression is specified, the system automatically adds an ELSE NULL.
If expr1 matches an expression in multiple WHEN clauses, only the expression following the first match is
assigned.
Syntax
CASE expr1
WHEN expr2 THEN expr3
{WHEN expr... THEN expr...}
ELSE expr
END
Where:
CASE starts the CASE statement. Must be followed by an expression and one or
more WHEN and THEN statements, an optional ELSE statement, and the END keyword.
WHEN specifies the condition to be satisfied.
THEN specifies the value to assign if the corresponding WHEN expression is satisfied.
ELSE specifies the value to assign if none of the WHEN conditions are satisfied. If omitted, ELSE NULL is
assumed.
END ends the CASE statement.
Example
CASE "TableHeading"."Column Name"
WHEN 'Col_Val1' THEN 'Val1'
2. WHEN 'Col_Val2' THEN 'Val2'
WHEN 'Col_Val3' THEN 'Val3'
ELSE "TableHeading"."Column Name"
END
In this example, the WHEN statements must reflect a strict equality.
CASE (If)
This form of the CASE statement evaluates each WHEN condition and if satisfied, assigns the value in the
corresponding THEN expression.
If none of the WHEN conditions are satisfied, it assigns the default value specified in the ELSE expression. If
no ELSE expression is specified, the system automatically adds an ELSE NULL.
Syntax
CASE
WHEN request_condition1 THEN expr1
{WHEN request_condition2 THEN expr2}
{WHEN request_condition... THEN expr...}
ELSE expr
END
Where:
CASE starts the CASE statement. Must be followed by one or more WHEN and THEN statements, an
optional ELSE statement, and the END keyword.
WHEN specifies the condition to be satisfied.
THEN specifies the value to assign if the corresponding WHEN expression is satisfied.
ELSE specifies the value to assign if none of the WHEN conditions are satisfied. If omitted, ELSE NULL is
assumed.
END ends the CASE statement.
Example
CASE
WHEN ("TableHeading"."ColumnName1"='Collaboration' AND "TableHeading"."ColumnName"='Value1')
THEN 'Val1'
WHEN ("TableHeading"."ColumnName1"='Collaboration' AND
"TableHeading1"."ColumnName2"='Value2') THEN 'Val2'
WHEN "TableHeading"."Column Name"='Value3' THEN 'Val3'
ELSE "TableHeading"."Column Name"
3. END
Note :
Unlike the Switch form of the CASE statement, the WHEN statements in the If form allow comparison operators.
In a CASE statement, AND has precedence over OR.