Skip to main content

CASE WHEN Formatting

CASE branches (WHEN/ELSE/END) are indented under the CASE keyword.

Modes: ALWAYS (default) expands all CASE expressions. NEVER keeps them inline. WIDTH_AWARE preserves original formatting but breaks when maxLineWidth is exceeded.

AND/OR suppression: AND/OR inside CASE WHEN conditions does NOT trigger line breaks, even when andOrNewline=ALWAYS. This keeps WHEN clauses readable.

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 t
select case when x > 1 then 'a' when x > 2 then 'b' else 'c' end from t

CASE WHEN without ELSE

select case when x > 1 then 'a' end from t
select case when x > 1 then 'a' end from t

AND 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 t
select case when a > 1 and b < 2 then 'x' else 'y' end from t

CASE in WHERE clause

select a from t where case when x > 1 then 1 else 0 end = 1
select a from t where case when x > 1 then 1 else 0 end = 1

CASE WHEN with clause newline off

select case when x > 1 then 'a' else 'b' end from t
select case when x > 1 then 'a' else 'b' end from t

CASE 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 t
select case when x > 1 then 'a' else 'b' end from t

CASE WHEN ALWAYS mode expands

ALWAYS mode (default) expands CASE even from compact input.

select case when x > 1 then 'a' else 'b' end from t
select case when x > 1 then 'a' else 'b' end from t

CASE WHEN WIDTH_AWARE compact

WIDTH_AWARE mode: user wrote compact — keep compact.

select case when x > 1 then 'a' else 'b' end from t
select case when x > 1 then 'a' else 'b' end from t

CASE WHEN WIDTH_AWARE vertical

WIDTH_AWARE mode: user wrote vertical (newline before WHEN) — expand.

select case
when x > 1 then 'a'
else 'b'
end from t
select case when x > 1 then 'a' else 'b' end from t

Multiple 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 t
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 t

CASE 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 t
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 t

Nested 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 t
select case when x > 1 then case when y > 2 then 'a' else 'b' end else 'c' end
from t