Skip to main content

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

A short CASE stays on one line​

It fits, so it does not break.

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

Line 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 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