SQL Server (T-SQL) Formatter
Paste a T-SQL query and get consistently formatted SQL. Bracketed [identifiers], @parameters and SQL Server clauses such as TOP and OPTION are all handled.
A SQL Server query, 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.
WITH ranked AS (SELECT [c].[CustomerID], [o].[OrderID], [o].[Total], ROW_NUMBER() OVER (PARTITION BY [c].[CustomerID] ORDER BY [o].[Total] DESC) AS rn FROM [dbo].[Customers] [c] INNER JOIN [dbo].[Orders] [o] ON [o].[CustomerID] = [c].[CustomerID] WHERE [o].[OrderDate] >= @StartDate AND [c].[Region] = @Region) SELECT TOP 100 * FROM ranked WHERE rn = 1 AND [Total] > @MinTotal ORDER BY [Total] DESC WITH
ranked AS (
SELECT
[c].[CustomerID],
[o].[OrderID],
[o].[Total],
ROW_NUMBER() OVER (
PARTITION BY
[c].[CustomerID]
ORDER BY
[o].[Total] DESC
) AS rn
FROM
[dbo].[Customers] [c]
INNER JOIN [dbo].[Orders] [o] ON [o].[CustomerID] = [c].[CustomerID]
WHERE
[o].[OrderDate] >= @StartDate
AND [c].[Region] = @Region
)
SELECT
TOP 100 *
FROM
ranked
WHERE
rn = 1
AND [Total] > @MinTotal
ORDER BY
[Total] DESC SQL Server syntax it handles
A generic formatter mangles dialect-specific syntax or refuses it outright. These are the SQL Server constructs this formatter is tested against.
@Parameter T-SQL parameters are detected and grouped by name.
[identifier] Bracketed identifiers keep their brackets and internal spacing.
TOP (n) Formatted as part of the SELECT clause rather than split off.
@@ROWCOUNT System variables are not reported as parameters.
SQL Server formatter FAQ
Are @parameters detected?
Yes, and system variables such as @@ROWCOUNT are correctly excluded, so only real parameters appear in the Parameters panel. Fill each parameter once and every occurrence in the query updates together.
Can it format a query with several CTEs?
Yes. Every WHERE clause in the query is listed by the CTE it belongs to, so you can pick one and edit it as an AND/OR tree or a node graph while the rest of the query stays untouched.
Is my query sent to a server?
No. Everything runs in your browser, so schema names, table names and any embedded values never leave your machine.
Are bracketed identifiers containing spaces or reserved words handled?
Yes. A name such as [Order Details] is read as one identifier, brackets and internal spacing included, so the space inside the name is never treated as a token boundary and a reserved word in brackets is never re-cased as a keyword.
Can it format a partial T-SQL statement?
Yes. A fragment works — including a bare WHERE [o].[OrderDate] >= @StartDate — because nothing depends on a complete parse. That is the common case when you have copied a predicate out of a long stored procedure to work on it in isolation.
Are query hints such as OPTION (RECOMPILE) preserved?
Yes. Hints are passed through in place rather than reordered or dropped, so the formatted query is still the query you were tuning and the plan you get back is the plan you were measuring.