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

Escape mode:
Try an example:
Characters: 0 Lines: 1 Mode: SQL Escape ('' quotes)

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.

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.

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

Escaping double quotes and identifiers

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.

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.

  1. 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.
  2. 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.
  3. 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.
  4. Run it inside a transaction: BEGIN; ... ROLLBACK;, then check the row count with SELECT and LENGTH().

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.

Related Tools

SQL Formatter· SQL Validator· SQL Minifier· SQL to JSON · MySQL Formatter · PostgreSQL Formatter · SQL Injection Prevention · PostgreSQL vs MySQL Syntax · Fixing MySQL Error 1064