How to format SQL in SQL Server Management Studio (SSMS)

For most of its life SQL Server Management Studio had no SQL formatter, which is why almost every answer you will find about this recommends a third-party add-in. That changed in SSMS 22.7: there is now a built-in formatter, still marked Preview, built on Microsoft's open-source ScriptDOM parser. It is genuinely good, it reads .editorconfig, and it is T-SQL only. This page covers the shortcut, the full option surface, and the two situations the built-in formatter still does not cover.

Short answer

Built in, from SSMS 22.7 onwards

Right-click in a query window and choose Format SQL (Preview), or press Ctrl+K, Ctrl+Q. On SSMS 21 and earlier there is no SQL formatter at all — the Ctrl+K, Ctrl+D you may remember from Visual Studio does not reformat T-SQL.

Windows
Ctrl+K, Ctrl+Q
Windows only
Need more control? Format a query now

Step by step in SQL Server Management Studio

  1. 01

    Check your SSMS version first

    The formatter needs SSMS 22.7 or later. Help → About shows the version. If you are on 21.x or 20.x the menu item below simply will not exist, and no setting turns it on — you need to upgrade or use one of the alternatives further down this page.

  2. 02

    Format on demand

    Three routes to the same command: right-click inside a T-SQL editor window and choose Format SQL (Preview); use Edit → Advanced → Format SQL (Preview); or press Ctrl+K, Ctrl+Q. Select a block first and only that block is reformatted, which is the usual way to tidy one statement inside a long script without touching the rest.

  3. 03

    Turn on format-on-save

    Tools → Options → SQL Formatter (Preview) → Formatting, then set Format on Save to True. This works for saved query windows and for files in SQL database projects, which is what makes formatting stop being something people forget before opening a pull request.

  4. 04

    Configure the style in Tools → Options

    Tools → Options → SQL Formatter (Preview) holds eight groups of settings: General, Alignment, Paths, Formatting, Indentation, Multiline, New Line and Spacing. Between them you get keyword casing, indent size and tabs-versus-spaces, whether commas lead or trail, whether semicolons are appended, and a per-clause switch for line breaks before FROM, WHERE, GROUP BY, HAVING, ORDER BY, JOIN and ON.

  5. 05

    Commit the style to the repository with .editorconfig

    Every option is also an .editorconfig key under a [*.sql] section — keyword_casing, comma_placement, indentation_size, include_semicolons, preserve_comments, new_line_before_where_clause and the rest. Where both exist, the .editorconfig value wins over the SSMS setting, so a team can standardise per project while each developer keeps their own defaults elsewhere. This is the setting worth reaching for: it is the only one that survives a new laptop.

SQL Server (T-SQL), before and after

The unformatted query below is the sample loaded into the editor above, and the output is exactly what this formatter returns for it with the default options. Nothing was tidied up by hand.

Pasted in
WITH recent_orders AS (SELECT o.order_id, o.customer_id, o.placed_at, o.total_amount FROM dbo.orders AS o WHERE o.placed_at >= @start_date AND o.status <> 'cancelled') SELECT TOP (100) c.company_name, COUNT(r.order_id) AS order_count, SUM(r.total_amount) AS lifetime_value FROM recent_orders AS r INNER JOIN dbo.customers AS c ON c.customer_id = r.customer_id WHERE c.region = @region GROUP BY c.company_name HAVING SUM(r.total_amount) > @min_value ORDER BY lifetime_value DESC;
Formatted output
WITH
  recent_orders AS (
    SELECT
      o.order_id,
      o.customer_id,
      o.placed_at,
      o.total_amount
    FROM
      dbo.orders AS o
    WHERE
      o.placed_at >= @start_date
      AND o.status <> 'cancelled'
  )
SELECT
  TOP (100) c.company_name,
  COUNT(r.order_id) AS order_count,
  SUM(r.total_amount) AS lifetime_value
FROM
  recent_orders AS r
  INNER JOIN dbo.customers AS c ON c.customer_id = r.customer_id
WHERE
  c.region = @region
GROUP BY
  c.company_name
HAVING
  SUM(r.total_amount) > @min_value
ORDER BY
  lifetime_value DESC;

What SSMS still won't do

None of this is a knock on SSMS — these are just the edges of what an in-editor formatter is for. They are also the reason there is a formatter at the top of this page.

  • It is T-SQL only. ScriptDOM parses Transact-SQL, so pasting a PostgreSQL, ClickHouse, Snowflake or BigQuery query into SSMS and pressing Ctrl+K, Ctrl+Q gets you nothing useful — a real problem if you work across engines from one editor.
  • It needs a parseable statement. A half-written WHERE clause pulled out of application logs, or a query with the ORM's placeholders still in it, will not format.
  • It is still marked Preview, and it only exists on 22.7 and later. Plenty of shops are pinned to an older SSMS by policy.
  • There is no way to try a style before committing to it — you change a setting, reformat, and undo if you dislike the result.

SSMS SQL formatting FAQ

What is the keyboard shortcut to format SQL in SSMS?

Ctrl+K, Ctrl+Q — pressed as a chord, Ctrl+K followed by Ctrl+Q while still holding Ctrl. It runs Format SQL (Preview) on the selection, or on the whole document if nothing is selected. It requires SSMS 22.7 or later.

Why is there no Format SQL option in my SSMS?

The command shipped in SSMS 22.7. On earlier versions the menu item does not exist and there is no preference that enables it. Check Help → About; if you are below 22.7, either upgrade or format elsewhere.

Does Ctrl+K, Ctrl+D format T-SQL in SSMS?

No. Ctrl+K, Ctrl+D is the Visual Studio shell's Format Document command that SSMS inherits, and it does not reformat T-SQL in a query window. The T-SQL formatter is a separate command on Ctrl+K, Ctrl+Q.

Can SSMS format PostgreSQL or Snowflake SQL?

No. The formatter is built on ScriptDOM, which parses Transact-SQL only. For any other dialect you need a formatter that carries that grammar — the one embedded on this page supports nineteen, including PostgreSQL, Snowflake, BigQuery and ClickHouse.

How do I enforce one SQL style across a team in SSMS?

Put the options in an .editorconfig file under a [*.sql] section and commit it. SSMS reads it and its values override each developer's local Tools → Options settings, so the repository defines the style rather than whoever last touched the file.

Can I format SQL without installing anything?

Yes — that is what the formatter above is for. It runs entirely in your browser, so nothing is uploaded and nothing is installed, which also makes it the practical option on a locked-down machine where you cannot add SSMS add-ins.

How do I wrap long SQL text in SSMS?

Word wrap changes only how long lines are displayed; it does not format or indent the SQL. To enable it, open Tools → Options → Text Editor, choose the relevant language or All Languages, and select Word wrap. Use Format SQL separately when you want to change the query's actual layout.