You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TiDB literal values include character literals, numeric literals, time and date literals, hexadecimal, binary literals, and NULL literals. This document introduces each of these literal values.
11
+
10
12
This document describes String literals, Numeric literals, NULL values, Hexadecimal literals, Date and time literals, Boolean literals, and Bit-value literals.
11
13
12
14
## String literals
@@ -28,11 +30,12 @@ Quoted strings placed next to each other are concatenated to a single string. Th
28
30
29
31
If the `ANSI_QUOTES` SQL MODE is enabled, string literals can be quoted only within single quotation marks because a string quoted within double quotation marks is interpreted as an identifier.
30
32
31
-
A binary string is a string of bytes. Each binary string has a character set and collation named `binary`. A non-binary string is a string of characters. It has a character set other than `binary` and a collation that is compatible with the character set.
33
+
The string is divided into the following two types:
32
34
33
-
For both types of strings, comparisons are based on the numeric values of the string unit. For binary strings, the unit is the byte. For non-binary strings, the unit is the character and some character sets support multibyte characters.
35
+
+ Binary string: It consists of a sequence of bytes, whose charset and collation are both `binary`, and uses **byte** as the unit when compared with each other.
36
+
+ Non-binary string: It consists of a sequence of characters and has various charsets and collations other than `binary`. When compared with each other, non-binary strings use **characters** as the unit. A charater might contian multiple bytes, depending on the charset.
34
37
35
-
A string literal may have an optional `character set introducer` and `COLLATE clause`, to designate it as a string that uses a specific character set and collation. TiDB only supports this in syntax, but does not process it.
38
+
A string literal may have an optional `character set introducer` and `COLLATE clause`, to designate it as a string that uses a specific character set and collation.
36
39
37
40
```
38
41
[_charset_name]'string' [COLLATE collation_name]
@@ -54,26 +57,23 @@ SELECT n'some text';
54
57
SELECT _utf8'some text';
55
58
```
56
59
57
-
Escape characters:
58
-
59
-
-`\0`: An ASCII NUL (X'00') character
60
-
-`\'`: A single quote (') character
61
-
-`\"`: A double quote (")character
62
-
-`\b`: A backspace character
63
-
-`\n`: A newline (linefeed) character
64
-
-`\r`: A carriage return character
65
-
-`\t`: A tab character
66
-
-`\z`: ASCII 26 (Ctrl + Z)
67
-
-`\\`: A backslash `\` character
68
-
-`\%`: A `%` character
69
-
-`\_`: A `_` character
60
+
To represent some special characters in a string, you can use escape characters to escape:
70
61
71
-
You can use the following ways to include quote characters within a string:
62
+
| Escape Characters | Meaning |
63
+
| :---------------- | :------ |
64
+
|\\0 | An ASCII NUL (X'00') character |
65
+
|\\' | A single quote `'` character |
66
+
|\\" | A double quote `"` character |
67
+
|\\b | A backspace character |
68
+
|\\n | A line break (newline) character |
69
+
|\\r | A carriage return character |
70
+
|\\t | A tab character |
71
+
|\\z | ASCII 26 (Ctrl + Z) |
72
+
|\\\\| A backslash `\` character |
73
+
|\\% | A `%` character |
74
+
|\\_| A `_` character |
72
75
73
-
- A `'` inside a string quoted with `'` may be written as `''`.
74
-
- A `"` inside a string quoted with `"` may be written as `""`.
75
-
- Precede the quote character by an escape character `\`.
76
-
- A `'` inside a string quoted with `"` needs no special treatment, and a `"` inside a string quoted with `'` needs no special treatment either.
76
+
If you want to represent `"` in the string surrounded by `'`, or `'` in the string surrounded by `"`, you do not need to use escape characters.
77
77
78
78
For more information, see [String Literals in MySQL](https://dev.mysql.com/doc/refman/5.7/en/string-literals.html).
79
79
@@ -89,11 +89,60 @@ Numeric literals can also be represented in scientific notation, such as `1.2E3,
89
89
90
90
For more information, see [Numeric Literals in MySQL](https://dev.mysql.com/doc/refman/5.7/en/number-literals.html).
91
91
92
-
## NULL values
92
+
## Date and time literals
93
+
94
+
Date and time literal values can be represented in several formats, such as quoted strings or as numbers. When TiDB expects a date, it interprets any of `'2017-08-24'`, `'20170824'` and `20170824` as a date.
95
+
96
+
TiDB supports the following date formats:
97
+
98
+
*`'YYYY-MM-DD'` or `'YY-MM-DD'`: The `-` delimiter here is not strict. It can be any punctuation. For example, `'2017-08-24'`, `'2017&08&24'`, `'2012@12^31'` are all valid date formats. The only special punctuation is '.', which is is treated as a decimal point to separate the integer and fractional parts. Date and time can be separated by `T` or a white space. For example, `2017-8-24 10:42:00` and `2017-8-24T10:42:00` represents the same date and time.
99
+
*`'YYYYMMDDHHMMSS'` or `'YYMMDDHHMMSS'`: For example, `'20170824104520'` and `'170824104520'` are regarded as `'2017-08-24 10:45:20'`. However, if you provide a value out of range, such as `'170824304520'`, it is not treated as a valid date.
100
+
*`YYYYMMDDHHMMSS` or `YYMMDDHHMMSS`: Note that these formats have no single or double quotes, but a number. For example, `20170824104520` is interpreted as `'2017-08-24 10:45:20'`.
101
+
102
+
DATETIME or TIMESTAMP values can be followed by a fractional part, used to represent microseconds precision (6 digits). The fractional part should always be separated from the rest of the time by a decimal point `.`.
103
+
104
+
The year value containing only two digits is ambiguous. It is recommended to use the four-digit year format. TiDB interpretes the two-digit year value according to the following rules:
105
+
106
+
* If the year value is in the range of `70-99`, it is converted to `1970-1999`.
107
+
* If the year value is in the range of `00-69`, it is converted to `2000-2069`.
108
+
109
+
For month or day values less than 10, `'2017-8-4'` is the same as `'2017-08-04'`. The same is true for Time. For example, `'2017-08-24 1:2:3'` is the same as `'2017-08-24 01:02:03'`.
110
+
111
+
When the date or time value is required, TiDB selects the specified format according to the length of the value:
112
+
113
+
* 6 digits: `YYMMDD`.
114
+
* 12 digits: `YYMMDDHHMMSS`.
115
+
* 8 digits: `YYYYMMDD`.
116
+
* 14 digits: `YYYYMMDDHHMMSS`.
117
+
118
+
TiDB supports the following formats for time values:
119
+
120
+
*`'D HH:MM:SS'`, or `'HH:MM:SS'`, `'HH:MM'`, `'D HH:MM'`, `'D HH'`, `'SS'`: `D` means days and the valid value range is `0-34`.
121
+
* A number in `HHMMSS` format: For example, `231010` is interpreted as `'23:10:10'`.
122
+
* A number in any of `SS`, `MMSS`, and `HHMMSS`formats can be regarded as time.
93
123
94
-
The `NULL` value means “no data”. NULL can be written in any letter case. A synonym is `\N` (case sensitive).
124
+
The decimal point of the Time type is also `.`, with a precision of up to 6 digits after the decimal point.
95
125
96
-
Be aware that the `NULL` value is different from values such as `0` for numeric types or the empty string `''` for string types.
126
+
See [MySQL date and time literals](https://dev.mysql.com/doc/refman/5.7/en/date-and-time-literals.html) for more details.
127
+
128
+
## Boolean Literals
129
+
130
+
The constants `TRUE` and `FALSE` are equal to 1 and 0 respectively, which are not case sensitive.
131
+
132
+
{{< copyable "sql" >}}
133
+
134
+
```sql
135
+
SELECT TRUE, true, tRuE, FALSE, FaLsE, false;
136
+
```
137
+
138
+
```
139
+
+------+------+------+-------+-------+-------+
140
+
| TRUE | true | tRuE | FALSE | FaLsE | false |
141
+
+------+------+------+-------+-------+-------+
142
+
| 1 | 1 | 1 | 0 | 0 | 0 |
143
+
+------+------+------+-------+-------+-------+
144
+
1 row in set (0.00 sec)
145
+
```
97
146
98
147
## Hexadecimal literals
99
148
@@ -126,8 +175,7 @@ mysql> select X'0aff';
126
175
+---------+
127
176
| X'0aff' |
128
177
+---------+
129
-
|
130
-
|
178
+
| 0x0aff |
131
179
+---------+
132
180
1 row inset (0.00 sec)
133
181
```
@@ -154,56 +202,6 @@ mysql> SELECT X'54694442';
154
202
1 row inset (0.00 sec)
155
203
```
156
204
157
-
## Date and time literals
158
-
159
-
Date and time values can be represented in several formats, such as quoted strings or as numbers. When TiDB expects a date, it interprets any of `'2015-07-21'`, `'20150721'` and `20150721` as a date.
160
-
161
-
TiDB supports the following formats for date values:
162
-
163
-
- As a string in either `'YYYY-MM-DD'` or `'YY-MM-DD'` format. The `-` delimiter is "relaxed" in syntax. Any punctuation character may be used as the delimiter between date parts. For example, `'2017-08-24'`, `'2017&08&24'` and `'2012@12^31'` are equivalent. The only delimiter recognized is the `.` character, which is treated as a decimal point to separate the integer and fractional parts. The date and time parts can be separated by `T` other than a space. For example, `2017-8-24 10:42:00` and `2017-8-24T10:42:00` are equivalent.
164
-
- As a string with no delimiters in either `'YYYYMMDDHHMMSS'` or `'YYMMDDHHMMSS'` format. For example, `'20170824104520'` and `'170824104520'` are interpreted as `'2017-08-24 10:45:20'`. But `'170824304520'` is illegal because the hour part exceeds the legal range.
165
-
- As a number in either `YYYYMMDDHHMMSS` or `YYMMDDHHMMSS` format, without single quotation marks or double quotation marks. For example, `20170824104520` is interpreted as `'2017-08-24 10:45:20'`.
166
-
167
-
A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision. The fractional part should always be separated from the rest of the time by a decimal point.
168
-
169
-
Dates containing two-digit year values are ambiguous. It is recommended to use the four-digit format. TiDB interprets two-digit year values using the following rules:
170
-
171
-
- Year values in the range of `70-99` are converted to `1970-1999`.
172
-
- Year values in the range of `00-69` are converted to `2000-2069`.
173
-
174
-
For values specified as strings that include date part delimiters, it is unnecessary to specify two digits for month or day values that are less than 10. `'2017-8-4'` is the same as `'2017-08-04'`. Similarly, for values specified as strings that include time part delimiters, it is unnecessary to specify two digits for hour, minute, or second values that are less than 10. `'2017-08-24 1:2:3'` is the same as `'2017-08-24 01:02:03'`.
175
-
176
-
In TiDB, the date or time values specified as numbers are interpreted according their length:
177
-
178
-
- 6 digits: `YYMMDD`
179
-
- 12 digits: `YYMMDDHHMMSS`
180
-
- 8 digits: `YYYYMMDD`
181
-
- 14 digits: `YYYYMMDDHHMMSS`
182
-
183
-
TiDB supports the following formats for time values:
184
-
185
-
- As a string in `'D HH:MM:SS'` format. You can also use one of the following “relaxed” syntaxes: `'HH:MM:SS'`, `'HH:MM'`, `'D HH:MM'`, `'D HH'`, or `'SS'`. Here D represents days and the legal value range is `0-34`.
186
-
- As a number in `'HHMMSS'` format. For example, `231010` is interpreted as `'23:10:10'`.
187
-
- A number in any of the `SS`, `MMSS` or `HHMMSS` format can be treated as time.
188
-
189
-
The time value can also include a trailing fractional part in up to 6 digits precision. The `.` character represents the decimal point.
190
-
191
-
For more information, see [Date and Time Literals in MySQL](https://dev.mysql.com/doc/refman/5.7/en/date-and-time-literals.html).
192
-
193
-
## Boolean literals
194
-
195
-
The constants `TRUE` and `FALSE` evaluate to 1 and 0 respectively, which are not case sensitive.
Bit-value literals are written using `b'val'` or `0bval` notation. The `val` is a binary value written using zeros and ones. A leading `0b` is case sensitive and cannot be written as `0B`.
0 commit comments