Skip to content

Commit ff871af

Browse files
authored
update literal-values.md (#3078)
Signed-off-by: lucklove <gnu.crazier@gmail.com>
1 parent 9e8c7cb commit ff871af

File tree

1 file changed

+83
-77
lines changed

1 file changed

+83
-77
lines changed

literal-values.md

Lines changed: 83 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
---
22
title: Literal Values
3-
summary: Learn how to use various literal values.
3+
summary: This article introduces the literal values ​​of TiDB SQL statements.
44
category: reference
55
aliases: ['/docs/dev/literal-values/','/docs/dev/reference/sql/language-structure/literal-values/']
66
---
77

88
# Literal Values
99

10+
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+
1012
This document describes String literals, Numeric literals, NULL values, Hexadecimal literals, Date and time literals, Boolean literals, and Bit-value literals.
1113

1214
## String literals
@@ -28,11 +30,12 @@ Quoted strings placed next to each other are concatenated to a single string. Th
2830

2931
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.
3032

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:
3234

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.
3437

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.
3639

3740
```
3841
[_charset_name]'string' [COLLATE collation_name]
@@ -54,26 +57,23 @@ SELECT n'some text';
5457
SELECT _utf8'some text';
5558
```
5659

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:
7061

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 |
7275

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.
7777

7878
For more information, see [String Literals in MySQL](https://dev.mysql.com/doc/refman/5.7/en/string-literals.html).
7979

@@ -89,11 +89,60 @@ Numeric literals can also be represented in scientific notation, such as `1.2E3,
8989

9090
For more information, see [Numeric Literals in MySQL](https://dev.mysql.com/doc/refman/5.7/en/number-literals.html).
9191

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.
93123

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.
95125

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+
```
97146

98147
## Hexadecimal literals
99148

@@ -126,8 +175,7 @@ mysql> select X'0aff';
126175
+---------+
127176
| X'0aff' |
128177
+---------+
129-
|
130-
|
178+
| 0x0aff |
131179
+---------+
132180
1 row in set (0.00 sec)
133181
```
@@ -154,56 +202,6 @@ mysql> SELECT X'54694442';
154202
1 row in set (0.00 sec)
155203
```
156204

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.
196-
197-
```sql
198-
mysql> SELECT TRUE, true, tRuE, FALSE, FaLsE, false;
199-
+------+------+------+-------+-------+-------+
200-
| TRUE | true | tRuE | FALSE | FaLsE | false |
201-
+------+------+------+-------+-------+-------+
202-
| 1 | 1 | 1 | 0 | 0 | 0 |
203-
+------+------+------+-------+-------+-------+
204-
1 row in set (0.00 sec)
205-
```
206-
207205
## Bit-value literals
208206

209207
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`.
@@ -243,3 +241,11 @@ mysql> SELECT b+0, BIN(b), HEX(b) FROM t;
243241
+------+--------+--------+
244242
3 rows in set (0.00 sec)
245243
```
244+
245+
## NULL Values
246+
247+
`NULL` means the data is empty, which is case-insensitive, and is synonymous with `\N` (case-sensitive).
248+
249+
> **Note:**
250+
>
251+
> `NULL` is not the same as `0`, nor the empty string `''`.

0 commit comments

Comments
 (0)