CASE WHEN Formatting
CASE branches (WHEN/ELSE/END) are indented under the CASE keyword.
A CASE that fits on one line stays on one line; one that does not gets its branches on their own
lines, indented under CASE.
AND/OR inside a WHEN condition does not force a break. The CASE decides the overall shape, so a
compound condition stays with its WHEN instead of fragmenting it.
Nesting: CASE expressions inside subqueries or functions are indented relative to their context. Nested CASEs accumulate indentation.
Basic CASE WHEN
select case when x > 1 then 'a' when x > 2 then 'b' else 'c' end from tselect case when x > 1 then 'a' when x > 2 then 'b' else 'c' end from tCASE WHEN without ELSE
select case when x > 1 then 'a' end from tselect case when x > 1 then 'a' end from tAND inside CASE WHEN does not break
AND/OR inside CASE WHEN conditions should NOT trigger line breaks.
select case when a > 1 and b < 2 then 'x' else 'y' end from tselect case when a > 1 and b < 2 then 'x' else 'y' end from tCASE in WHERE clause
select a from t where case when x > 1 then 1 else 0 end = 1select a from t where case when x > 1 then 1 else 0 end = 1CASE WHEN NEVER mode keeps inline
NEVER mode keeps CASE inline even with vertical input.
select case
when x > 1 then 'a'
else 'b'
end from tselect case when x > 1 then 'a' else 'b' end from tCASE WHEN ALWAYS mode expands
ALWAYS mode (default) expands CASE even from compact input.
select case when x > 1 then 'a' else 'b' end from tselect case when x > 1 then 'a' else 'b' end from tA short CASE stays on one line
It fits, so it does not break.
select case when x > 1 then 'a' else 'b' end from tselect case when x > 1 then 'a' else 'b' end from tLine breaks you wrote are not preserved by default
The same CASE written across four lines comes back on one: with preserveBreaks: off, layout is
decided by width, not by where you pressed enter.
select case
when x > 1 then 'a'
else 'b'
end from tselect case when x > 1 then 'a' else 'b' end from tMultiple CASEs in same SELECT
Each CASE expression is independently expanded.
select case when a > 1 then 'x' else 'y' end, case when b > 2 then 'p' when c > 3 then 'q' else 'r' end from tselect
case when a > 1 then 'x' else 'y' end,
case when b > 2 then 'p' when c > 3 then 'q' else 'r' end
from tCASE with many WHEN branches
select case when status = 'A' then 'Active' when status = 'I' then 'Inactive' when status = 'P' then 'Pending' when status = 'R' then 'Rejected' when status = 'D' then 'Deleted' when status = 'S' then 'Suspended' when status = 'X' then 'Expired' when status = 'C' then 'Completed' else 'Unknown' end from tselect
case
when status = 'A' then 'Active'
when status = 'I' then 'Inactive'
when status = 'P' then 'Pending'
when status = 'R' then 'Rejected'
when status = 'D' then 'Deleted'
when status = 'S' then 'Suspended'
when status = 'X' then 'Expired'
when status = 'C' then 'Completed'
else 'Unknown'
end
from tNested CASE
Nested CASEs accumulate indentation. Inner CASE indented relative to outer WHEN.
select case when x > 1 then case when y > 2 then 'a' else 'b' end else 'c' end from tselect case when x > 1 then case when y > 2 then 'a' else 'b' end else 'c' end
from t