Utvikling · verktøy

SQL-formaterer

Lim inn en SQL-spørring og formater vanlige klausuler, innrykk og nøkkelord lokalt.

Kjøres i nettleseren. Ingen opplasting.

Om verktøyet

SQL-formaterer sem complicação.

Formatereren gjenkjenner vanlige SQL-klausuler, anførte strenger og kommentarer, og legger til leselige linjeskift og innrykk. Dette er formatering etter beste evne, ikke en parser, syntaksvalidator eller databasetilkobling; kontroller resultatet før kjøring.

Veiledning

Slik bruker du SQL-formaterer

The SQL Formatter takes a pasted query and lays it out with line breaks and indentation, and it can turn common keywords into capitals. It works on the text only. It has no dialect setting and uses one generic set of rules for every query, so it is a best-effort tidy-up and not a parser. It never connects to a database, never runs your SQL and does not check that the query is correct.

Slik gjør du

  1. Paste your SQL into the SQL query box. The limit is 200,000 characters, and an empty box shows an error message.
  2. Leave Uppercase keywords ticked to capitalise the keywords the tool knows, or untick it to keep your own capitalisation.
  3. Press Format SQL. The tidy version appears in the Formatted SQL box. Main clauses start on new lines, and commas at the top level start a new indented line.
  4. Read the result before you use it, then press Copy SQL. Clear empties both boxes.

Eksempler

A simple select

Du skriver inn
SQL-spørring
select id,name from users where active=1 and note='from where' order by name
Store nøkkelord
ja
Du får
Formatert SQL
select 'it''s' as s /* block */ from t -- end

Each main clause begins a new line. Spaces are put around the equals signs, and the text inside the quotes, including the words from and where, is left exactly as typed.

Kjørt med verktøyets egen kode.

A join and a group, with capitals on

Du skriver inn
SQL-spørring
select u.id, count(o.id) as orders from users u left join orders o on o.user_id = u.id group by u.id order by orders desc limit 10;
Store nøkkelord
ja
Du får
Formatert SQL
SELECT u.id, count(o.id) AS orders FROM users u left JOIN orders o ON o.user_id = u.id group BY u.id ORDER BY orders DESC LIMIT 10;

The line breaks are right, but the capitals are uneven. The first word of a two-word clause, left and group, stays lowercase while join and by are capitalised. This is how the code behaves today.

Kjørt med verktøyets egen kode.

Quotes and comments, capitals off

Du skriver inn
SQL-spørring
select 'it''s' as s /* block */ from t -- end
Store nøkkelord
nei
Du får
Formatert SQL
select 'it''s' as s /* block */ from t -- end

The doubled quote inside the string is kept, and both comments are kept on their own lines. With the box unticked, none of the words are changed to capitals.

Kjørt med verktøyets egen kode.

Når det er nyttig

  • Make a long one-line query readable before you read it or share it in a message.
  • Put a query into a consistent layout before adding it to documentation or code review.
  • Quickly spot the clauses of a query you did not write.
  • Tidy SQL copied out of logs without sending it to an online service.

Begrensninger

  • There is no dialect choice. The code understands single-quoted strings, double-quoted and backtick names, square-bracket names, and both -- and /* */ comments. It has no special rules for any particular database product.
  • Things it does not know: backslash escapes inside strings, # comments, dollar-quoted blocks such as PostgreSQL function bodies, and nested block comments. Text in those can be mis-split or re-laid out, so check it.
  • Some operators and signs are spaced oddly. A negative number such as -1 becomes - 1, and the JSON operator ->> becomes -> >, which changes the text of the query.
  • Only the keywords it knows are capitalised, the list is short, and long queries with deep nesting, window functions or CASE blocks may get uneven indentation. It never runs or validates your SQL.

Tips

  • Read the formatted version once before running it. Compare it against the original if the query uses JSON operators, negative numbers or special string quotes.
  • Untick Uppercase keywords if your team writes keywords in lowercase, because that also avoids the half-capitalised joins.
  • Format a long script one statement at a time if it contains function bodies in dollar quotes, since those are not recognised.

Kilder

Vanlige spørsmål

Sobre SQL-formaterer.

Which SQL dialects does it support?

It has no dialect setting. One generic set of rules is applied to every query. It reads the common quoting styles, including backticks and square brackets, and ordinary comments, but it has no knowledge of any single database's special syntax.

Does it run or check my SQL?

No. It never connects to a database and does not validate syntax. A wrong query stays wrong after formatting, and a correct one is not proven correct, so review the output before running it.

Does it change what my query does?

It aims only to change spacing, line breaks and keyword case. In a few cases it also alters spacing around signs, such as turning ->> into -> >. Compare the result with the original if your query uses such operators.

Why are some keywords capitalised and others not?

Only a fixed list of keywords is capitalised. In the two-word clauses LEFT JOIN, GROUP BY and PARTITION BY, the first word is not in that list, so it stays as you typed it. Untick the box to keep everything unchanged.

What happens to comments and strings?

Text inside single or double quotes and in -- and /* */ comments is kept as written. Doubled quotes such as 'it''s' work. A backslash before a quote does not, so a string like 'it\'s' can end early.

How big can the query be?

Up to 200,000 characters. An empty box, or text longer than that, shows an error message instead of output. The limit applies to the characters you paste, not to the number of statements.

Is my SQL uploaded?

No. The page states that the formatting runs locally in your browser. Even so, avoid pasting passwords or secrets into any text box.

Hvordan bruker du SQL-formaterer?

Lim inn en SQL-spørring og formater vanlige klausuler, innrykk og nøkkelord lokalt.

Lastes dataene mine opp til en server?

Nei. Behandlingen skjer i nettleseren din, og opplysningene dine lastes ikke opp.