Xerobit

SQL to HTML: Syntax Highlighted Markup

Paste a query and get HTML in which every keyword, string, number, comment and operator is wrapped in a span with a short class, so SELECT 1 becomes a kw span followed by a num span. The sql to html converter outputs plain markup with six classes (kw, str, num, cmt, op, id), so the colors are yours to set in CSS. Tokenizing happens in your browser and the query is not uploaded.

Paste your SQL query here.

Adds <pre><code class="language-sql"> around the output.

HTML Output

<pre><code class="language-sql"><span class="kw">SELECT</span> id<span class="op">,</span> name <span class="kw">FROM</span> users <span class="kw">WHERE</span> active <span class="op">=</span> <span class="num">1</span><span class="op">;</span></code></pre>

How to use the sql to html converter

  1. Paste the query into SQL input. Empty or whitespace-only input returns an error.
  2. Tick Wrap in pre/code tags to get a complete <pre><code class="language-sql"> block, or untick it for bare spans.
  3. Copy the HTML output into your page or template.
  4. Add CSS rules for .kw, .str, .num, .cmt, .op and .id (see the class table) so the tokens show color.

Token classes and what each one matches

ClassMatchesExample rule
.kwOne of 228 keywords, types and function names, any letter casecolor: #005cc5;
.strSingle-quoted strings, with '' as an escaped quotecolor: #032f62;
.numIntegers, decimals, exponents such as 1.5e-3, and hex such as 0x1Fcolor: #005cc5;
.cmt-- line and /* block */ commentscolor: #6a737d;
.opEvery other single character: , ; ( ) = < > * .color: #d73a49;
.idDouble-quoted and backtick-quoted identifierscolor: #6f42c1;

Plain words that are not keywords, such as table and column names, get no span at all.

Worked examples with real output

As an sql to html example, the query SELECT name FROM users WHERE id = 7 AND status <> 'x'; converts to:

<span class="kw">SELECT</span> name <span class="kw">FROM</span> users <span class="kw">WHERE</span> id <span class="op">=</span> <span class="num">7</span> <span class="kw">AND</span> status <span class="op">&lt;</span><span class="op">&gt;</span> <span class="str">'x'</span><span class="op">;</span>

Notice that name, users, id and status are not in the keyword list, so they stay plain, and that <> becomes two operator spans, each with its characters escaped as &lt; and &gt;. The string 'It''s done' stays one .str span.

Escaping and string rules

The sql to html output HTML-escapes every piece of text before it is placed in the output: &, <, > and double quotes become entities, so a query containing <script> inside a string cannot inject markup. Single quotes are left as they are, which is safe in element text.

Strings follow the SQL standard (ISO/IEC 9075): a single quote inside a string is written twice. The scanner reads to the next ' and, if another one follows at once, keeps going. An unterminated string runs to the end of the input and is still wrapped in one span. Double quotes are delimited identifiers in standard SQL, which is why they get .id and not .str. Backticks are the MySQL form of the same thing.

Edge cases and known limits

  • Context-free keywords. The scan is lexical, not a parser. key, level, text, date, year, left and count are highlighted wherever they appear, even as column names. Quote them to get the .id class instead. name, value, id and status are not keywords.
  • Operators. Each symbol is its own span, so >= is two spans.
  • Dotted names. In a.id the dot is an operator span and both names are plain.
  • Numbers. A number followed directly by a word, as in 3else, gives a number span and then the word; an exponent is taken only when digits follow the e.
  • Whitespace. Non-breaking spaces from pasted web text are treated as whitespace, so keywords next to them still highlight. All whitespace and newlines are kept as typed.
  • Not handled. MySQL # comments, PostgreSQL dollar-quoted strings and prefixed strings such as N'x' get no special treatment; they stay safe but are classed by their parts.

Using the markup on a page

The HTML Standard suggests marking the language of a code block with a class that starts with language-, which is why the wrapper is <code class="language-sql">. That class lets Markdown renderers and client-side highlighters recognise the block, but it does not add colors on its own; you still need your CSS for the six token classes. Running sql to html ahead of time suits static sites, since no highlighter script has to run in the browser. To tidy the query first, use the SQL formatter or SQL minify. For the HTML side, see the HTML minifier, and for tabular data the CSV to HTML table and SQL to JSON tools.

Worked examples

ExampleInputResult
Simple SELECTSQL input: SELECT id, name FROM users WHERE active = 1;; Wrap in pre/code tags: yes<pre><code class="language-sql"><span class="kw">SELECT</span> id<span class="op">,</span> name <span class="kw">FROM</span> users <span class="kw">WHERE</span> active <span class="op">=</span> <span class="num">1</span><span class="op">;</span></code></pre>
INSERT with stringsSQL input: INSERT INTO logs (msg) VALUES ('It''s done');; Wrap in pre/code tags: no<span class="kw">INSERT</span> <span class="kw">INTO</span> logs <span class="op">(</span>msg<span class="op">)</span> <span class="kw">VALUES</span> <span class="op">(</span><span class="str">'It''s done'</span><span class="op">)</span><span class="op">;</span>
Query with commentsSQL input: -- fetch active users / SELECT * FROM users /* all columns */ / WHERE status = 'active';; Wrap in pre/code tags: yes<pre><code class="language-sql"><span class="cmt">-- fetch active users</span> / <span class="kw">SELECT</span> <span class="op">*</span> <span class="kw">FROM</span> users <span class="cmt">/* all columns */</span> / <span class="kw">WHERE</span> status <span class="op">=</span> <span class="str">'active'</span><span class="op">;</span></code></pre>

FAQ

Why is my column name highlighted as a keyword?

The tool has a fixed list of 228 words and does not know where a word is used. Words such as key, level, text, date, year and count match everywhere. Wrap the name in double quotes or backticks to get the .id class.

Why are table and column names not colored?

Only keywords, strings, numbers, comments, operators and quoted identifiers get a span. Ordinary names such as users, name or status are left as plain text to keep the markup small.

What does the wrap option add?

It surrounds the spans with a pre element holding a code element of class language-sql. Without it you get only the spans, which suits a code element you already have.

Is the output safe to paste into a page?

Ampersands, angle brackets and double quotes in the SQL are escaped, so query text cannot create tags. Only the span, pre and code tags added by the tool are real markup.

Which comment styles are recognised?

-- to the end of the line and /* ... */ blocks, as in the SQL standard. An unclosed block comment runs to the end of the input. MySQL # comments are not recognised.

Does it validate or reformat my SQL?

No. It does not check syntax and it keeps spaces and line breaks exactly as typed. Use a formatter first if you want the layout cleaned up.

How are hex numbers and exponents handled?

0x1F is one number span, and 1.5e-3 is one number span. An e not followed by digits, as in 3else, is left for the next word.