SQL Escape Online — Escape Single Quotes in SQL Queries
Escape single quotes, double quotes, backslashes, and special characters for SQL queries. All processing happens in your browser — your data never leaves your machine.
🔐 SQL Escape
How to Use the SQL Escape Tool
SQL Escape ('' quotes)
This is the standard SQL escape method. Every single quote ' inside a string literal is doubled to ''. This is the most common and widely supported SQL escape technique, used by MySQL, PostgreSQL, SQLite, Oracle, and most other databases.
Input: It's a great day Output: It''s a great day
SQL Escape (\' backslash)
Some databases (notably MySQL with NO_BACKSLASH_ESCAPES disabled) support C-style backslash escaping. This mode prepends a backslash before single quotes, double quotes, backslashes, and control characters like newlines and tabs.
Input: It's a "test" with \backslash Output: It\'s a \"test\" with \\backslash
JSON Escape
Use this when embedding string values inside JSON or when constructing JSON that will be stored in a SQL column. This mode escapes double quotes, backslashes, newlines, tabs, and other control characters using standard JSON escape sequences.
Input: {"name": "O'Brien", "path": "C:\data"}
Output: {\"name\": \"O'Brien\", \"path\": \"C:\\data\"}
Unescape All
Reverts escaped strings back to their original form. Handles both doubled-quote SQL escaping ('' → ') and backslash escaping (\' → ', \\ → \), as well as JSON escape sequences.
Input: It''s a \"test\" with \\backslash Output: It's a "test" with \backslash
Auto-Escape
As you type in the input box, the output updates automatically with a small debounce delay of 300ms. You can also press Ctrl+Enter to trigger escaping immediately.
What SQL Escaping Actually Solves
A SQL query treats quotes as both punctuation and data: in WHERE name = 'Ada' the first quote opens a string literal and the last closes it. Put a quote inside the value — O'Brien — and the literal ends early, so the parser reads the rest as syntax. Escaping marks that quote as data: standard SQL doubles it ('O''Brien'), and MySQL's default mode also accepts a backslash (O\'Brien). Both store the same value. That is the whole job — a quoting concern, not a security boundary.
Why Escaping Is Not a Security Strategy
SQL injection happens when untrusted input is concatenated into a query, so the input closes the literal early and appends SQL of its own. Escaping the input first is the tempting fix, and it fails for three reasons.
- Escaping is context-blind. A query holds string literals, quoted identifiers,
LIKEpatterns andORDER BYnames; doubling quotes covers the first only. - One missed path is a total failure. Every value on every code path must be escaped, including the error handler and next quarter's new branch.
- Server settings change the escape character: MySQL stops honouring backslashes under
NO_BACKSLASH_ESCAPES, so such code breaks after a configuration change.
A parameterized query never builds the string at all: the SQL text and the values travel separately, so the parser never receives the value as code.
cur.execute("SELECT * FROM users WHERE email = '" + email + "'") -- injectable
cur.execute("SELECT * FROM users WHERE email = %s", (email,)) -- parameterized
Binding also handles ordinary quoting for free; escaping is a fallback for cases where binding is impossible, never a replacement — see the SQL injection prevention guide.
Escape Rules Differ by Database Dialect
There is no single "SQL escape" — match the mode to the engine that parses the string.
- PostgreSQL, Oracle, SQL Server, SQLite, standard SQL: double the single quote; backslash is ordinary.
- MySQL / MariaDB default: backslash escapes are active, so
\'works too. A literal backslash is written\\, and\n,\t,\0mean something inside strings. - Server settings move the line: MySQL with
NO_BACKSLASH_ESCAPESmakes only doubling safe; PostgreSQL withstandard_conforming_strings = offkeeps the legacy behaviour unlessE'...'is used. - Identifiers differ again: standard SQL doubles a double quote in a name, MySQL uses backticks, SQL Server brackets. Validate names against an allow-list; escaping is not a fix.
SQL Escape Character Reference: Single Quote, Double Quote and Backslash
Every escaping bug traces back to one of three characters and one question: which engine is parsing the string. This reference is the short version of the rules above, with the exact literal to type.
Escaping a single quote (') in every major dialect
- PostgreSQL, SQL Server, SQLite, Oracle, standard SQL: double it.
O'BrienbecomesO''Brien. A backslash is an ordinary character and needs no treatment. - MySQL / MariaDB (default mode):
\'and''both work, but\'is the one the client relies on. If the server runs withNO_BACKSLASH_ESCAPES, only doubling is valid. - Inside a PostgreSQL
E'...'string: backslash escapes are active even though plain literals ignore them, soE'O\'Brien'is legal andE'O''Brien'is still legal too.
Escaping double quotes and identifiers
- In a string value, a double quote needs no escaping in any of the five engines — the delimiter is the single quote. Escaping it anyway is how
\"ends up stored literally in a MySQL value. - An identifier uses a different delimiter: double quotes in PostgreSQL and standard SQL (double them to embed one), backticks in MySQL (double them), square brackets in SQL Server (double the closing bracket).
- Never build an identifier from user text at all — check it against a fixed allow-list first. No amount of escaping makes an arbitrary identifier safe.
Backslashes, newlines and control characters
In MySQL and in PostgreSQL E'...' strings a literal backslash must be doubled to \\, a newline can be written \n, a tab \t, and a NUL byte \0. In the other engines those sequences are just the two characters typed, so a value copied from a MySQL dump into PostgreSQL keeps the backslashes visibly.
The dangerous shape is a value that ends in a backslash: in a backslash-escaping dialect the trailing \ consumes the closing quote and the statement swallows whatever follows. Doubling the backslash before you emit the string is the fix, and switching the escape mode in the tool above is how you check it.
Once the literal is correct, confirm the statement itself parses: paste the result into the SQL validator, or format it with the MySQL and PostgreSQL formatters to see the quoting survive a re-parse.
When Hand-Escaping Is the Right Tool
Binding covers values, not everything. Escaping earns its place when the deliverable is text you emit rather than input you accept.
- Ad-hoc queries typed into
psqlor the MySQL client, with no driver to bind anything. - Generating a
.sqlscript or migration, where the literal must be correct in the file because there is no parameter channel at run time. - Exporting rows as
INSERTstatements, where every string value needs its quotes doubled. - Dynamic DDL and identifiers: names cannot be bound, and escaping is not enough either — check the name against a fixed allow-list and quote it with the dialect's identifier rules.
Every item is about emitting text, not accepting input. A value that comes from a user and lands in a query belongs in a placeholder.
How to Verify an Escaped String Before You Run It
Both under-escaping and over-escaping survive a glance and both fail later, quietly: a row stores O''Brien with the quotes showing, or a statement dies on a syntax error seen only in a log. Four checks catch most of it.
- Round-trip it. Switch to Unescape All and paste the output back; you should recover the original exactly. A surviving quote means the escaping ran twice or the wrong mode was used.
- Compare character counts. Doubling the quotes in a value with n apostrophes adds exactly n characters; any other jump means something was escaped that should not have been.
- Test hostile values:
','',\', a trailing backslash before the closing quote ('abc\), a newline. A trailing backslash is the classic breaker: in a backslash-escaping dialect it consumes the closing quote. - Run it inside a transaction:
BEGIN; ... ROLLBACK;, then check the row count withSELECTandLENGTH().
Escaping in Bulk: Columns of Values and INSERT Scripts
The tool processes the whole input, so a column of a thousand values is one paste.
One value per line. Escape the column and copy the output back. Doubling is safe across a list as long as no line already holds a doubled quote; keep values one per line rather than comma-separated, so a value containing a comma cannot shift the columns.
Multi-row INSERT. Escape the values, then assemble the statement. One INSERT ... VALUES (...), (...); beats a thousand separate inserts because the server plans it once. The failure mode is one unescaped quote halfway down the list: the statement fails and the error points at the wrong row, so test on a single row first.
For a large file, use the engine's bulk loader (COPY, LOAD DATA INFILE, BULK INSERT) with a configured quoting and escape character — same principle as binding: let the layer that understands the format do the encoding.
Frequently Asked Questions
How do I escape a single quote in SQL?
Double it. In PostgreSQL, SQL Server, SQLite, Oracle and standard SQL, O'Brien is written O''Brien inside a string literal — two apostrophes, no backslash. MySQL also accepts \' because backslash escaping is on by default, but doubling works there too and keeps the literal portable across engines.
How do I escape a single quote in MySQL?
Write \' or ''. The backslash form is what the mysql client and most drivers produce, while '' is the standard form. Watch the session mode: with NO_BACKSLASH_ESCAPES set on the server, \' is no longer an escape at all, so doubling is the only safe choice for a script that may run on a differently configured server.
How do I escape a single quote in SQL Server?
Double it, the same as standard SQL: 'It''s done' is a single string containing an apostrophe. There is no backslash escape in T-SQL — a backslash is a literal character — so do not carry MySQL-style \' into SQL Server, and remember that a leading N'...' prefix changes only the type to nvarchar, not the quoting rules.
Why does my query still break after escaping the quotes?
Almost always a trailing backslash or a double-escaped value. In a backslash-escaping dialect a value ending in \ consumes the closing quote and the parser keeps reading into the next line. Test with the hostile cases from the verification checklist: a lone quote, a doubled quote, a backslash-quote pair, a value ending in a backslash, and a newline.
Do I need to escape double quotes in SQL?
Not inside a normal string literal: the single quote is the delimiter, so double quotes are ordinary characters in PostgreSQL, MySQL, SQL Server, Oracle and SQLite. They matter only when an identifier is delimited with double quotes, and in that case embed one by doubling it — or, better, avoid building identifiers from input entirely.
Is escaping enough to prevent SQL injection?
No. Escaping protects the text you emit, not the queries you accept: one missed value, one concatenated identifier or one differently configured server breaks it. The injection defence is parameter binding — placeholders for values, an allow-list for identifiers. See our SQL injection prevention guide, and use this tool for the cases where you are writing a script rather than running a parameterised query.
Can I escape a whole column of values or an INSERT script?
Yes — the tool processes the entire input at once. Paste one value per line for a column, or a full multi-row INSERT to normalise every literal inside it. Keep values one per line rather than comma-separated so a value containing a comma cannot shift columns, and re-run Unescape on the output to confirm it round-trips before you run the script.