CASE WHEN ... THEN ... END without an ELSE branch returns NULL for
unmatched rows. Often unintended - the author thought the WHEN
conditions were exhaustive but they aren't, or the downstream code
can't handle NULL.
Should fail
SELECT CASE
WHEN status = 'paid' THEN 1
WHEN status = 'pending' THEN 0
END AS paid_flag
FROM orders;
Should pass
SELECT CASE
WHEN status = 'paid' THEN 1
WHEN status = 'pending' THEN 0
ELSE NULL
END AS paid_flag
FROM orders;
Implementation hints
- Multi-line rule.
- Detect
CASE ... END without ELSE between.
- Test with nested CASE expressions - make sure outer CASE without ELSE
still triggers when inner CASE has ELSE.
- Severity:
warning.
Estimated LOC: ~35 code + ~25 test.
CASE WHEN ... THEN ... ENDwithout anELSEbranch returnsNULLforunmatched rows. Often unintended - the author thought the
WHENconditions were exhaustive but they aren't, or the downstream code
can't handle NULL.
Should fail
Should pass
Implementation hints
CASE...ENDwithoutELSEbetween.still triggers when inner CASE has ELSE.
warning.Estimated LOC: ~35 code + ~25 test.