SSMS SQL Formatter: Format T-SQL Without an Add-in

🛠️Part of the SSMS Complete Guide, installing, configuring and fixing SQL Server Management Studio.In: Server & Configuration

Somebody hands you a 40-line query written on one line, or a procedure that three people have indented three different ways, and you need to read it before you run it. SSMS formats T-SQL itself now. The SQL Formatter arrived in SSMS 22.7 and has been generally available since 22.10.2, so there is no add-in to buy and no reason to paste production SQL into a website.

⚡Ctrl+K, Ctrl+Q formats the whole query window, or only the lines you have selected. Ctrl+Z undoes it in one step. The rules live under Tools → Options → SQL Formatter.

Format a Query

  1. Open the query in SSMS 22.7 or later.
  2. Select the part you want formatted, or select nothing to format the whole window.
  3. Press Ctrl+K, Ctrl+Q. Or right-click in the editor and choose Format SQL, or use Edit → Advanced → Format SQL.
SSMS 22 query editor with a long one-line T-SQL query and the right-click menu open, Format SQL at the top with the shortcut Ctrl+K, Ctrl+Q.

Here is a backup-age query written the way it usually arrives, on one line. Format SQL sits at the top of the right-click menu.


What It Changed

The same query after Format SQL: one column per line, SELECT, FROM, WHERE, GROUP BY, HAVING and ORDER BY on their own lines, keywords in upper case, LEFT JOIN rewritten as LEFT OUTER JOIN, each dateadd argument on its own line and a semicolon added at the end.

Same query, one keystroke later, on the default settings. Each clause starts its own line and each column gets its own row, which is what makes it readable. It also changed a few things worth knowing before you format someone else’s code:

  • Keywords went to upper case: select became SELECT, case became CASE.
  • Function names did not. max, count and dateadd stayed lower case, because built-in function casing is set to Preserve.
  • LEFT JOIN became LEFT OUTER JOIN. Same join, longer spelling.
  • A semicolon was added after the last statement.
  • Each dateadd argument went onto its own line. Correct, but long for a three-argument function; the Multiline settings control it.
  • The comments were kept, word for word.

None of that changes what the query does. It only changes how it reads. If you do not like the result, Ctrl+Z puts the original back in one step.


Make It Format Your Way

SSMS Options searched for formatter: the SQL Formatter pages Alignment, Formatting, General, Indentation, Multiline, New Line and Spacing, with the Formatting page open showing Keyword casing UPPERCASE, Built-in function casing Preserve, Terminate block statements, Preserve comments and Comma placement Trailing.

Open Tools → Options and type formatter in the search box. The SQL Formatter settings are split across seven pages: Alignment, Formatting, General, Indentation, Multiline, New Line and Spacing. Select a setting and the Preview box shows example T-SQL with that value applied, so you can see a change before you commit to it.

The settings that decide whether you can live with it:

  • Keyword casing and Built-in function casing, on the Formatting page.
  • Comma placement, trailing or leading.
  • Indent with tabs or spaces, and the indent size.
  • Column alias style: AS, an equals sign, or leave it as written.
  • New lines around JOIN and ON.
  • Format on Save, on the Formatting page, for saved query files and SQL projects.

For a team, put the same rules in an .editorconfig file next to the scripts (Microsoft’s Format T-SQL in SSMS page has an example). Its values override each person’s SSMS settings for the files it covers, so a shared scripts folder formats the same way on everyone’s machine, and a Git diff shows real changes, not re-indentation.


Why Not Just Use an Online Formatter

Because the query you most want to read is usually the one from production, and pasting it into a website sends your table names, column names and sometimes the literal values in the WHERE clause to somebody else’s server. Formatting used to be a reason to buy an add-in or take that risk. It is now built into the tool you already have open, and it never leaves the machine.


Common Questions

Which SSMS version has the SQL Formatter?

22.7 or later. It was a preview until 22.10.2, when it became generally available. Install and Update SSMS gets you onto the current release.

Can formatting change what my query does?

It should not. It changes layout and casing, and spellings like LEFT JOIN to LEFT OUTER JOIN that mean the same thing. Read the diff before you save over a procedure anyway, the same as any other change.

How do I undo the formatting?

Ctrl+Z, straight after. The whole format is one undo step.

Can I format only part of a script?

Yes. Select the lines first. With nothing selected it formats the whole window.


Related

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *