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.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
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.