Skip to content

Commit c7f88fe

Browse files
committed
Document string literal syntax
1 parent 8295cd8 commit c7f88fe

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

sql/identifiers.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ There is a considerable variation in implementations:
3535
- `".."` [Redshift][]
3636
- `` `..` `` [Spark][]
3737
- `".."`, `` `..` ``, `[..]` [SQLite][sqlite-keywords]
38-
- `".."`, `[..]` [Transact-SQL][]
38+
- `".."`<sup>3</sup>, `[..]` [Transact-SQL][]
3939

4040
Notes:
4141

4242
1. when ANSI_QUOTES mode enabled
4343
2. when MSSQL mode enabled
44+
3. unless QUOTED_IDENTIFIER option has been set OFF
4445

4546
[bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical
4647
[db2]: https://www.ibm.com/docs/en/db2/9.7?topic=elements-identifiers

sql/strings.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

sql/syntax.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ Reference of SQL syntax variations.
44

55
- [Identifiers](./identifiers.md)
66
- [Parameters](./parameters.md)
7+
- [Strings](./strings.md)
78
- [SELECT](./select.md)

0 commit comments

Comments
 (0)