T-SQL Formatter

Paste SQL Server code to get clean, consistently indented T-SQL. Handles CTEs, CASE expressions, joins, comments, [bracketed] names, N'...' strings, and GO batch separators. Your SQL never leaves your browser.

Tip: press Ctrl+Enter in the input box to format.

How to use it

Paste a query, script, or stored procedure into the left box and click Format SQL. Choose keyword case, function case, indent size, and where AND/OR go; the result updates when you change an option. Copy the result and paste it back into SSMS or Azure Data Studio.

Formatting only changes whitespace and letter case of keywords and built-in functions. Table names, column names, aliases, string literals, and comments are left exactly as you wrote them.

FAQ

Is my SQL uploaded anywhere?

No. The formatter is JavaScript running in your browser, and the site's security policy blocks the page from sending data to any server. It works offline once loaded.

Does it handle GO batch separators?

Yes. A line containing only GO (or GO 5) is treated as a batch separator, the same way SSMS and sqlcmd treat it, and each batch is formatted separately.

Will it change what my query does?

It shouldn't: only spacing, line breaks, and the case of SQL keywords and built-in function names change. Uppercasing keywords is safe even on case-sensitive databases, because keywords are never case-sensitive. Still, review the result before running it in production.

Why did I get a "could not format" message?

Usually there's an unclosed quote, bracket, or /* comment. Fix that and try again. Very unusual syntax can also confuse the parser; if so, format the script in smaller pieces.

Which formatter does this use?

The open-source sql-formatter library in its Transact-SQL mode, served from this site, plus handling for SQL Server's GO separator.

Practice T-SQL on a realistic database

The Bramble Cycles SQL Workbook has 120 hands-on SQL Server exercises on a three-store bike shop database, from your first SELECT to window functions and index tuning. Every exercise has a check line and an answer key.