CASE (IF-THEN-ELSE), Part 1 of 3
The CASE function gives you IF-THEN-ELSE capability. It evaluates a list of conditions and returns one value from several possible values. CASE can be used in two ways: simple and search.
Simple CASE compares one value (the input_value) to a list of other values and returns the first match:
CASE input_value
WHEN match_condition THEN result_value
...
ELSE result_value
END
Search CASE compares to multiple conditions and returns a result from the first condition that is true:
CASE
WHEN search_condition THEN result_value
...
ELSE result_value
END
> For the simple CASE, a match is found when input_value = match_condition.
> For the search CASE, search_condition compares two values with a boolean operator.
> Multiple WHEN conditions are used, but only one ELSE clause is allowed.
> All values must be of the same type or implicitly convertible to the same type.
> Access does not support CASE. Instead it uses SWITCH.
> DB2, MySQL, Oracle, SQL Server, and Sybase support CASE.
> Oracle uses CASE and DECODE.