Firebird Documentation Index → Firebird 1.5 Language Ref. Update → Miscellaneous language elements → CASE construct |
Table of Contents
Find a more recent version at Firebird 5.0 Language Reference: CASE
Available in: DSQL, ESQL, PSQL
Added in: 1.5
Description: A CASE construct returns exactly one value from a number of possibilities. There are two syntactic variants:
The simple CASE, comparable to a Pascal case
or a C switch
.
The searched CASE, which works like a series of
“if ... else if ... else if
” clauses.
Syntax:
CASE<expression>
WHEN<exp1>
THENresult1
WHEN<exp2>
THENresult2
... [ELSEdefaultresult
] END
When this variant is used, <expression>
is compared
to <exp1>
, <exp2>
etc.,
until a match is found, upon which the corresponding result is returned. If there is no
match and there is an ELSE clause,
defaultresult
is returned. If there is no match and no
ELSE clause, NULL
is returned.
The match is determined with the “=” operator, so if
<expression>
is NULL
, it won't match
any of the <expN>
s, not even those that are
NULL
.
The results don't have to be literal values: they may also be field or variable names,
compound expressions, or NULL
literals.
Example:
select name, age, case upper(sex) when 'M' then 'Male' when 'F' then 'Female' else 'Unknown' end, religion from people
Syntax:
CASE WHEN<bool_exp1>
THENresult1
WHEN<bool_exp2>
THENresult2
... [ELSEdefaultresult
] END
Here, the <bool_expN>
s are tests that give a ternary
boolean result: true
, false
, or
NULL
. The first expression evaluating to TRUE
determines the result. If no expression is TRUE
and there is an
ELSE clause, defaultresult
is returned. If
no expression is TRUE
and there is no ELSE clause,
NULL
is returned.
As with the simple CASE, the results don't have to be literal
values: they may also be field or variable names, compound expressions, or
NULL
literals.
Example:
CanVote = case when Age >= 18 then 'Yes' when Age < 18 then 'No' else 'Unsure' end;
Firebird Documentation Index → Firebird 1.5 Language Ref. Update → Miscellaneous language elements → CASE construct |