// strings reference

SQL backslash escapes in strings

The same literal 'a\tb' is a tab on MySQL and four characters everywhere else, the root of many migration bugs and LIKE-pattern surprises.

Категория
Strings
Returns
string
In this category
65 of 172

Синтаксис

'a\tb'  -- MySQL: tab
PostgreSQL: needs E'a\tb'

Each block is a separate form. The support table and the examples show which engine takes which.

Поддержка движка

ДвижокSupportЗаметки
SQL Server Да Supported under this name.
PostgreSQL Да Supported under this name.
MySQL Да Supported under this name.
SQLite Да Supported under this name.
Oracle Да Supported under this name.

Примеры

MySQL

SELECT 'a\tb' AS s;  -- a (tab) b

PostgreSQL

SELECT E'a\tb' AS escaped, 'a\tb' AS plain;  -- tab vs a\tb

Oracle

SELECT 'a\tb' AS s FROM dual;  -- a\tb, backslash is literal

Полезно знать

  • MySQL processes backslash escapes in string literals by default; SET sql_mode = NO_BACKSLASH_ESCAPES turns that off, and existing data's meaning shifts with it.
  • PostgreSQL follows the standard: 'a\tb' is a literal backslash-t. The escape-processing form is E'a\tb' (or the \n CHR/CHAR equivalents).
  • Oracle, SQL Server and SQLite never process backslashes in literals: code written for MySQL carries stray backslashes into them.
  • MySQL's LIKE also uses backslash as its default escape character, so matching a literal backslash takes '\\\\' in the pattern. Portable SQL avoids backslash escapes and builds characters with CHR(10) or CHAR(10).

Questions about Backslash escapes in strings

Why is my backslash literal different on PostgreSQL?

Standard strings treat backslashes literally; only E'' strings process escapes. MySQL processes them by default, which is the difference you are seeing.

How do I write a portable newline in a literal?

Do not use backslashes; build it: CHAR(13) + CHAR(10) on SQL Server, CHR(10) on Oracle, char(10) on SQLite, E'\n' on PostgreSQL when you accept engine-specific syntax.

What does NO_BACKSLASH_ESCAPES change?

MySQL stops processing backslashes in literals: 'a\tb' becomes the literal text a\tb. Existing queries and stored data that rely on escapes behave differently.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.