CASE operator #
CASE [expr] [WHEN expr THEN expr]
...
[ELSE expr]
END
CASE construct implements a basic condition operator. It has two forms:
-
CASE with expressionCASEreturns the result of theTHENclause if the result of its expression matches the result of the firstWHENclause. -
CASE without expressionCASEreturns the result of theTHENclause if its correspondingWHENclause evaluates to true.
For both cases, if no matches are found, the result of the ELSE clause will be returned or NULL
if the ELSE clause is not defined. All expression types must match.
-- case expr with else
select
case 0
when 1 then 1
when 2 then 2
else 3
end;
[3]
-- case expr
select
case 0
when 1 then 1
when 2 then 2
end;
[null]
-- case without expr
select
case
when id = 2 then 'found'
else 'not found'
end
from test;
["not found", "found", "not found"]