|
| 1 | +# Strings |
| 2 | + |
| 3 | +SQL standard supports single-quoted strings `'..'` with repeated quote `''` used for escaping. |
| 4 | +The real world implementations have lots of variation: |
| 5 | + |
| 6 | +- [BigQuery][]: |
| 7 | + - `".."`, `'..'`, `"""..."""`, `'''..'''` (backslash `\` used for escaping) |
| 8 | + - `R".."`, `r'''..'''` the same as above, but with `r` or `R` prefix (backlashes not used for escaping) |
| 9 | + - `B".."`, `b'''..'''` the same as above, but with `b` or `B` prefix (backlashes not used for escaping) |
| 10 | + - `RB".."`, `br'''..'''` the same as above, but with additional `r` or `R` prefix (backlashes not used for escaping) |
| 11 | +- [DB2][]: |
| 12 | + - `'..'` (two single quotes `''` are used for escaping) |
| 13 | + - `X'..'` a hex string (no escaping) |
| 14 | + - `U&'..'` an unicode string (two single quotes `''` are used for escaping) |
| 15 | + - `G'..'`, `N'..'` a graphic string |
| 16 | + - `GX'..'` a graphic hex string (no escaping) |
| 17 | + - `UX'..'` an UCS-2 graphic string (no escaping) |
| 18 | +- [Hive][]: `'..'`, `".."` (backslash `\` used for escaping) |
| 19 | +- [MariaDB][]: |
| 20 | + - `'..'` (backslash `\`<sup>1</sup> or repeated single-quote `''` used for escaping) |
| 21 | + - `x'..'`, `X'..'` [hex string][mariadb-hex] |
| 22 | +- [MySQL][]: |
| 23 | + - `'..'`, `".."`<sup>2</sup> (backslash `\`<sup>1</sup> or repeated quote (`''` or `""`) used for escaping) |
| 24 | + - `x'..'`, `X'..'` [hex string][mysql-hex] |
| 25 | +- [N1QL][]: `".."` (backslash `\` used for escaping) |
| 26 | +- [PL/SQL][]: |
| 27 | + - `'..'` (two single quotes `''` are used for escaping) |
| 28 | + - `N'..'`, `n'..'` a string using a natural character set |
| 29 | + - `Q'x..x'`, `q'x..x'` where `x` is a custom delimiter character |
| 30 | + - `q'{..}'`, `q'[..]'`, `q'<..>'`, `q'(..)'` special handling for certain delimiters in above syntax |
| 31 | +- [PostgreSQL][]: |
| 32 | + - `'..'` (two single quotes `''` are used for escaping) |
| 33 | + - `E'..'`, `e'..'` string with C-style escapes (backslash `\` or repeated single-quote `''` used for escaping) |
| 34 | + - `U&'..'`, `u&'..'` string with unicode escapes |
| 35 | + - `$$..$$`, `$delim$..$delim$` dollar-quoted string with optional custom delimiters |
| 36 | + - `B'..'`, `b'..'` bit string |
| 37 | + - `X'..'`, `x'..'` hex string |
| 38 | +- [Redshift][]: `'..'` |
| 39 | +- [Spark][]: |
| 40 | + - `'..'` (backslash `\` used for escaping) |
| 41 | + - `X'..'` hex string |
| 42 | +- [SQLite][]: |
| 43 | + - `'..'` (two single quotes `''` are used for escaping) |
| 44 | + - `X'..'`, `x'..'` hex string |
| 45 | +- [Transact-SQL][]: |
| 46 | + - `'..'` (two single quotes `''` are used for escaping) |
| 47 | + - (`".."`<sup>3</sup>) |
| 48 | + - `N'..'` (`N".."`<sup>3</sup>) unicode strings |
| 49 | + |
| 50 | +### Notes: |
| 51 | + |
| 52 | +1. unless the SQL_MODE has been set to NO_BACKSLASH_ESCAPES. |
| 53 | +2. unless ANSI_QUOTES mode is enabled. |
| 54 | +3. if the QUOTED_IDENTIFIER option has been set OFF. |
| 55 | + |
| 56 | +[bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#string_and_bytes_literals |
| 57 | +[db2]: https://www.ibm.com/docs/en/db2/9.7?topic=elements-constants |
| 58 | +[hive]: https://cwiki.apache.org/confluence/display/hive/languagemanual%20types#LanguageManualTypes-StringsstringStrings |
| 59 | +[mariadb]: https://mariadb.com/kb/en/string-literals/ |
| 60 | +[mariadb-hex]: https://mariadb.com/kb/en/hexadecimal-literals/ |
| 61 | +[mysql]: https://dev.mysql.com/doc/refman/8.0/en/string-literals.html |
| 62 | +[mysql-hex]: https://dev.mysql.com/doc/refman/8.0/en/hexadecimal-literals.html |
| 63 | +[n1ql]: https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/literals.html#strings |
| 64 | +[pl/sql]: https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements003.htm#i42617 |
| 65 | +[postgresql]: https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS |
| 66 | +[redshift]: https://docs.aws.amazon.com/redshift/latest/dg/r_Examples_with_character_types.html |
| 67 | +[spark]: https://spark.apache.org/docs/latest/sql-ref-literals.html#string-literal |
| 68 | +[sqlite]: https://www.sqlite.org/lang_expr.html#literal_values_constants_ |
| 69 | +[transact-sql]: https://docs.microsoft.com/en-us/sql/t-sql/data-types/constants-transact-sql?view=sql-server-ver15 |
0 commit comments