SQL Escape String
ED-07Paste text to escape it as a SQL string literal for T-SQL, MySQL or PostgreSQL, or paste a literal to read the plain text. For application code, use parameterized queries instead.
Runs in your browser. Nothing is uploaded.
Text or SQL string literal
Result
How to use it
- Paste your text into the input box.
- Choose the dialect: T-SQL, MySQL or PostgreSQL.
- Press Escape. Untick Add surrounding quotes if you only want the escaped body.
- Tick the prefix box for N'...' in T-SQL or E'...' in PostgreSQL if you need it.
- Copy the result into your script.
How each dialect escapes
T-SQL and standard PostgreSQL strings escape a single quote by doubling it, so O'Brien becomes O''Brien and nothing else changes. MySQL, with its default settings, also uses backslash escapes, so the tool escapes the backslash, both quote marks, NUL, line breaks and Ctrl+Z. With the PostgreSQL prefix, the literal becomes E'...', in which backslashes are escapes, so the tool escapes them and writes control characters as \n, \t or \xNN.
What the prefix options do
In T-SQL, N before the opening quote makes the literal Unicode (nvarchar). Without it, characters outside the database's code page can turn into question marks. In PostgreSQL, E before the quote switches on backslash escapes. MySQL needs no prefix for this, so the box is ignored for it. Unescape reverses each form and accepts the prefix and quotes if you paste a whole literal.
Why parameters are the right tool for applications
This page is for one-off scripts, data fixes and ad hoc queries. In application code, escaping by hand is easy to get wrong and depends on server settings, such as MySQL's NO_BACKSLASH_ESCAPES mode or PostgreSQL's standard_conforming_strings. A parameterized query sends the value separately from the SQL, so the value cannot change the structure of the query.
Error messages and what to do
| Message | What it means | Fix |
|---|---|---|
Single quote at line 1, column 3 is not doubled. In a SQL string literal a quote is written as two quotes (''). | Unescape found a lone single quote inside the text, which would end the literal. | Double it, or press Escape on the original text. |
The string literal is never closed: there is no closing single quote. | The text starts like a quoted literal but has no closing quote. | Add the final single quote, or remove the opening one. |
PostgreSQL text cannot contain the NUL character (U+0000), so there is no way to write it in a string literal. | The input has a NUL character, which PostgreSQL does not allow in text. | Remove it, or store the data in a bytea column. |
Questions people ask
How do I escape a single quote in SQL Server?
Double it. In T-SQL, a single quote inside a string literal is written as two single quotes, so 'O''Brien' holds the text O'Brien. Paste the text here with T-SQL selected and the tool does it, and can add the N prefix.
How do I escape a single quote in MySQL?
Either double it ('') or put a backslash before it (\'). This tool writes the backslash form, together with escapes for backslashes, NUL, line breaks and Ctrl+Z. If your server runs in NO_BACKSLASH_ESCAPES mode, use the T-SQL setting, which only doubles quotes.
How do I escape a string in PostgreSQL?
By default, double each single quote, and backslashes are plain characters. If you need backslash escapes such as \n, use the E prefix, as in E'line one\nline two'. Tick the prefix box and the tool escapes the backslashes and quotes for it.
Is escaping enough to prevent SQL injection?
In application code, no. Escaping depends on server and connection settings and is easy to miss in one place. Use parameterized queries or prepared statements so values never become part of the SQL text. Use this tool for one-off scripts you run yourself.
When do I need the N prefix in T-SQL?
Use it when the value contains characters outside the code page of a varchar column or variable, such as Cyrillic or emoji, and the target is nvarchar. Without N, the literal is converted to a code page and unmappable characters become question marks.
Why can't PostgreSQL strings hold a NUL character?
PostgreSQL text values cannot contain the NUL character (U+0000) at all, in any literal form. The tool reports an error for it. If you need to store arbitrary bytes, use a bytea column instead.
About this tool
T-SQL and standard PostgreSQL strings double each single quote. MySQL, with its default settings, uses backslash escapes, so backslashes, both quote marks, NUL, line breaks and Ctrl+Z are escaped. The prefix option writes N'...' for T-SQL, which keeps non-ASCII characters in an nvarchar value, or E'...' for PostgreSQL, which makes backslash escapes active. Escaping is for one-off scripts and data fixes. It is not a substitute for parameterized queries, which are the way to pass user input to a database. MySQL in NO_BACKSLASH_ESCAPES mode and PostgreSQL with standard_conforming_strings turned off read backslashes differently from what is shown here.