SQL Server formatter (T-SQL)
Format T-SQL for SQL Server and Azure SQL. Bracketed names, @variables, #temp tables and GO batches are read the way SQL Server reads them, and your script never leaves your device.
Every layout setting of the formatter, grouped like Editor › Formatting in the SQLly app. The style above is the starting point; anything you change here is marked.
Drop a .sql file here to open it. It is read in your browser, not uploaded.
SELECT
c.CustomerID,
COUNT(*) AS Orders INTO #recent
FROM
dbo.Orders o
JOIN dbo.Customers c ON c.CustomerID = o.CustomerID
WHERE
o.OrderDate >= DATEADD(day, -30, GETDATE())
GROUP BY
c.CustomerID;
SELECT
r.CustomerID,
r.Orders,
@@rowcount AS Total
FROM
#recent r
WHERE
r.Orders > @min
ORDER BY
r.Orders DESC;
GO
DROP TABLE IF EXISTS #recent;
GO
Loading the formatter…
Shortcuts in the editor: Ctrl+Enter formats and Ctrl+Shift+Enter minifies (⌘ instead of Ctrl on a Mac). Your settings are remembered in this browser and kept in the page address, so a link shares your style – never your SQL.
What the formatter does with SQL Server SQL
- Bracketed names stay exact.
[Order Details]and[dbo].[Orders]are quoted names,]]escapes included. Set Quoting to Always quote and the formatter adds brackets; Remove unnecessary quotes drops them only from plain names that are not reserved words, soShared.[User]keeps its brackets. Quote only when required also adds them where a reserved word is used as a name:Shared.UserbecomesShared.[User]. - Variables and temp tables are names.
@since,@@ROWCOUNT,#recent,##sharedandOUTPUT $actionare each read as one token, so nothing gets split apart. - GO batches keep their own line. A
GOline, with an optional repeat count such asGO 5and in any letter case, always stays on a line by itself. A column that happens to be calledgois not mistaken for one. - T-SQL clauses lay out properly.
TOP,CROSS APPLY,MERGE … WHEN [NOT] MATCHED,OUTPUTand table hints such asWITH (NOLOCK)keep their meaning, andN'…'Unicode strings are never touched. - Auto-detect knows T-SQL when it sees bracketed names,
@parameters or aGOline. - Minify keeps GO. Collapsing a script onto one line still leaves each
GOalone on its line, because SQL Server tools require it.
Good to know
- Procedural T-SQL stays valid and the statements inside a procedure are laid out one by one, but
BEGIN … END,IFandWHILEblocks are not re-indented. - SQL containing ODBC escape sequences such as
{fn NOW()}is returned unchanged, with a note saying where the formatter stopped.
Examples
SQL Server before and after
select c.CustomerID,count(*) as Orders into #recent from dbo.Orders o join dbo.Customers c on c.CustomerID=o.CustomerID where o.OrderDate>=dateadd(day,-30,getdate()) group by c.CustomerID; select r.CustomerID,r.Orders,@@rowcount as Total from #recent r where r.Orders>@min order by r.Orders desc; go drop table if exists #recent; go
SELECT
c.CustomerID,
COUNT(*) AS Orders INTO #recent
FROM
dbo.Orders o
JOIN dbo.Customers c ON c.CustomerID = o.CustomerID
WHERE
o.OrderDate >= DATEADD(day, -30, GETDATE())
GROUP BY
c.CustomerID;
SELECT
r.CustomerID,
r.Orders,
@@rowcount AS Total
FROM
#recent r
WHERE
r.Orders > @min
ORDER BY
r.Orders DESC;
GO
DROP TABLE IF EXISTS #recent;
GO
The output uses the Standard style with 4-space indents, exactly as the formatter above produces it. Switch the style to Compact for denser SQL or Expanded for leading commas and one item per line. The formatter docs cover the details.
SQL Server formatter FAQ
Does the T-SQL formatter handle GO batch separators?
Yes. A GO line stays on its own line, including GO with a repeat count such as GO 5, and Minify keeps it there too. A column named go inside a statement is not treated as a separator.
Will it change my [bracketed] names?
No. Bracketed names are kept exactly as written unless you pick a different Quoting option in the advanced settings. Remove unnecessary quotes only unquotes plain names that are not reserved words (so [User] and [Order] keep their brackets), which is safe because SQL Server names are case-insensitive under the default collation.
Can it format scripts with temp tables and variables?
Yes. #temp and ##global temp tables, @variables, @@ system functions and $action are read as single names, so the formatter lays the script out instead of refusing it.
Does it work for Azure SQL?
Yes. Azure SQL Database and Azure SQL Managed Instance use the same T-SQL dialect, so pick SQL Server (T-SQL) or leave the dialect on auto-detect.
Is my SQL sent to a server?
No. The formatter is compiled to WebAssembly and runs inside your browser tab. Your SQL is never uploaded, logged or stored, and once the page has loaded it keeps working offline.
Can formatting break my query?
It is built not to. After formatting, SQLly checks its output token by token against your input. If anything other than whitespace and the casing you asked for would change, it gives your SQL back exactly as you wrote it and tells you why. SQL that does not parse cleanly, such as an unterminated string, is also left untouched, with the line and column of the problem.
Can I share my formatting settings with my team?
Yes. Every setting, including the advanced ones, is kept in the page address after the # sign, so copying the address or pressing Copy settings link shares your exact style. The link holds the settings only, never your SQL.
Other dialects
- Any dialect (auto-detect)
- PostgreSQL formatter
- MySQL formatter
- SQLite formatter
- Oracle SQL formatter
- Snowflake SQL formatter
- BigQuery SQL formatter
- Redshift SQL formatter
Working with SQL Server every day? See what the SQLly desktop app does for SQL Server, or browse all features.
Like the formatter? Meet the rest of SQLly
A native SQL workbench for macOS, Windows and Linux with IntelliSense that knows your schema, guardrails for risky queries, and this formatter built into the editor.