|
|
Using the CASE Expression
With a
CASEexpression, it is possible to specify a conditional value. Depending on the result of one or more conditional expressions, theCASEexpression can return different values.The rules for
CASEexpressions are fully described in the Mimer SQL Reference Manual, CASE Expression.Case Expression Examples
The following select statements presents two examples of how
CASEexpressions can be used.Simple Case Expression
Give a textual description to the DISPLAY_ORDER column and display them in the numeric order:
SELECT category_id, CASE display_order WHEN 10 THEN 'FIRST' WHEN 20 THEN 'SECOND' WHEN 30 THEN 'THIRD' WHEN 40 THEN 'FOURTH' ELSE 'UNKNOWN' END, format FROM formats ORDER BY category_id, display_order, format;Returns:
CATEGORY_ID FORMAT FIRST DVD Audio SECOND Audio CD THIRD Cassette FOURTH Vinyl FIRST Hardcover SECOND Paperback THIRD Audio CD FOURTH Audio Cassette FIRST DVD Video SECOND VideoThis form of a case expression is known as a simple case expression, in which an operand is compared to a list of values.
If there is a match in one of the when clauses, the result is the value to the right of the then clause.
If none of these matches, the value in the else clause is returned.
If there is no else clause in a case expression and no when clause matches, a null value is returned.
Case Expression
The other form of the case expression can be seen in the following example.
Display the word "UNKNOWN" if the EXCHANGE_RATE value is undefined (i.e. NULL); and display the word "PARITY" if the rate is 1 to 1; otherwise do not display anything:
SELECT currency, CASE WHEN exchange_rate IS NULL THEN 'UNKNOWN' WHEN exchange_rate = 1.0 THEN 'PARITY' ELSE '' END FROM currencies WHERE code LIKE 'A%';Returns:
CURRENCY UAE Dirhams Afghanis Leke Armenian Drams UNKNOWN Netherlands Antillian Guilders Kwanza UNKNOWN ... ...In this form it is possible that more than one of the when clauses evaluates to true, in which case the value in the first (from left) of the matching clauses is returned.
|
Mimer Information Technology AB Voice: +46 18 780 92 00 Fax: +46 18 780 92 40 info@mimer.se |
|
|