Skip to content

[Feature Request]: Case Statement Formatting #22959

Description

Feature Description

Case Statements with multiple cases and conditions can currently get very messy, as each condition is aligned with the WHEN statement, but each WHEN is on the same line as the previous THEN. The intention with this request is that others in the community can also include their preferred formats.

In general, I format CASE statements as follows:

  • Each WHEN on a new line (including the first one)
  • Each condition on a new line (unless it's the only one
  • THEN on a new line, indented one level
  • END on a new line, level with CASE

The following illustrates these points:

Select
  Case
    When
      a = 1
      And c <> 0 
      Then 'a' 
    When
      b = 1
      And c <> 0 
      Then 'b' 
    When c = 1 
      Then 'c' 
  End As colname
From tbl;

Problem and Motivation

This is a spin-off from #22764

Having this feature would reduce the compliance friction with our team's established formatting standards, allowing queries to be quickly scanned and understood by all team members.

The sample query from the description currently gets formatted as follows, which can quickly get out of hand with the extreme indentation:

Select Case When a = 1
                 And c <> 0 Then 'a' When b = 1
                                          And c <> 0 Then 'b' When c = 1 Then 'c' End As colname
From   tbl;

Related Area

  • Connection dialog (SQL Server | Azure browse/Fabric browse)
  • Query editor
  • Query results panel
  • Object Explorer
  • GitHub Copilot integration
  • Preview/Edit data
  • Table Designer
  • Schema Designer
  • Schema Compare
  • Local SQL Server Container provisioning
  • SQL database in Fabric provisioning
  • DACPAC/BACPAC export/import
  • SQL Database projects
  • Query Plan Visualizer
  • Other (please describe below)

If you selected "Other", please describe the area

No response

Confirmation

  • I have searched existing feature requests and couldn't find a match
  • I want to help implement this feature

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions