Table 12.15 Cast Functions and OperatorsCast函数和运算符
Cast functions and operators enable conversion of values from one data type to another.强制转换函数和运算符支持将值从一种数据类型转换为另一种数据类型。
CONVERT()
with a USING
clause converts data between character sets:CONVERT()
和USING
子句在字符集之间转换数据:
CONVERT(expr
USINGtranscoding_name
)
In MySQL, transcoding names are the same as the corresponding character set names.在MySQL中,转码名称与对应的字符集名称相同。
Examples:例如:
SELECT CONVERT('test' USING utf8mb4); SELECT CONVERT(_latin1'Müller' USING utf8mb4); INSERT INTO utf8mb4_table (utf8mb4_column) SELECT CONVERT(latin1_column USING utf8mb4) FROM latin1_table;
To convert strings between character sets, you can also use 要在字符集之间转换字符串,还可以使用CONVERT(
syntax (without expr
, type
)USING
), or CAST(
, which is equivalent:expr
AS type
)CONVERT(
语法(不使用),或expr
, type
)CAST(
,这相当于:expr
AS type
)
CONVERT(string
, CHAR[(N
)] CHARACTER SETcharset_name
) CAST(string
AS CHAR[(N
)] CHARACTER SETcharset_name
)
Examples:例如:
SELECT CONVERT('test', CHAR CHARACTER SET utf8mb4); SELECT CAST('test' AS CHAR CHARACTER SET utf8mb4);
If you specify 如果如图所示指定 CHARACTER SET
as just shown, the character set and collation of the result are charset_name
charset_name
and the default collation of charset_name
. CHARACTER SET
,则结果的字符集和排序规则是charset_name
charset_name
和charset_name
的默认排序规则。If you omit 如果省略了CHARACTER SET
, the character set and collation of the result are defined by the charset_name
character_set_connection
and collation_connection
system variables that determine the default connection character set and collation (see Section 10.4, “Connection Character Sets and Collations”).CHARACTER SET
,则结果的字符集和排序规则由charset_name
character_set_connection
和character_set_connection
系统变量定义,这些变量确定默认的连接字符集和排序规则(请参阅第10.4节,“连接字符集和排序规则”)。
A COLLATE
clause is not permitted within a CONVERT()
or CAST()
call, but you can apply it to the function result. For example, these are legal:CONVERT()
或CAST()
调用中不允许使用COLLATE
子句,但可以将其应用于函数结果。例如,这些是合法的:
SELECT CONVERT('test' USING utf8mb4) COLLATE utf8mb4_bin; SELECT CONVERT('test', CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_bin; SELECT CAST('test' AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_bin;
But these are illegal:但这些都是非法的:
SELECT CONVERT('test' USING utf8mb4 COLLATE utf8mb4_bin); SELECT CONVERT('test', CHAR CHARACTER SET utf8mb4 COLLATE utf8mb4_bin); SELECT CAST('test' AS CHAR CHARACTER SET utf8mb4 COLLATE utf8mb4_bin);
Normally, you cannot compare a 通常,不能以不区分大小写的方式比较BLOB值或其他二进制字符串,因为二进制字符串使用二进制字符集,而二进制字符集与lettercase概念没有排序规则。BLOB
value or other binary string in case-insensitive fashion because binary strings use the binary
character set, which has no collation with the concept of lettercase. To perform a case-insensitive comparison, first use the 要执行不区分大小写的比较,请首先使用CONVERT()
or CAST()
function to convert the value to a nonbinary string. CONVERT()
或CAST()
函数将值转换为非二进制字符串。Comparisons of the resulting string use its collation. 结果字符串的比较使用其排序规则。For example, if the conversion result collation is not case-sensitive, a 例如,如果转换结果排序规则不区分大小写,则LIKE
operation is not case-sensitive. LIKE
操作不区分大小写。That is true for the following operation because the default 这对于以下操作是正确的,因为默认的utf8mb4
collation (utf8mb4_0900_ai_ci
) is not case-sensitive:utf8mb4
排序规则(utf8mb4_0900_ai_ci
)不区分大小写:
SELECT 'A' LIKE CONVERT(blob_col
USING utf8mb4) FROMtbl_name
;
To specify a particular collation for the converted string, use a 要为转换的字符串指定特定的排序规则,请在COLLATE
clause following the CONVERT()
call:CONVERT()
调用后使用COLLATE
子句:
SELECT 'A' LIKE CONVERT(blob_col
USING utf8mb4) COLLATE utf8mb4_unicode_ci FROMtbl_name
;
To use a different character set, substitute its name for 要使用不同的字符集,请在前面的语句中将其名称替换为utf8mb4
in the preceding statements (and similarly to use a different collation).utf8mb4
(类似地,使用不同的排序规则)。
CONVERT()
and CAST()
can be used more generally for comparing strings represented in different character sets. CONVERT()
和CAST()
通常用于比较不同字符集中表示的字符串。For example, a comparison of these strings results in an error because they have different character sets:例如,比较这些字符串会导致错误,因为它们具有不同的字符集:
mysql>SET @s1 = _latin1 'abc', @s2 = _latin2 'abc';
mysql>SELECT @s1 = @s2;
ERROR 1267 (HY000): Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (latin2_general_ci,IMPLICIT) for operation '='
Converting one of the strings to a character set compatible with the other enables the comparison to occur without error:将其中一个字符串转换为与另一个字符串兼容的字符集可以使比较无误地进行:
mysql> SELECT @s1 = CONVERT(@s2 USING latin1);
+---------------------------------+
| @s1 = CONVERT(@s2 USING latin1) |
+---------------------------------+
| 1 |
+---------------------------------+
For string literals, another way to specify the character set is to use a character set introducer. 对于字符串文字,指定字符集的另一种方法是使用字符集引入器。前面例子中的_latin1
and _latin2
in the preceding example are instances of introducers. _latin1
和_latin2
是引导器的例子。Unlike conversion functions such as 与CAST()
, or CONVERT()
, which convert a string from one character set to another, an introducer designates a string literal as having a particular character set, with no conversion involved. CAST()
或CONVERT()
等转换函数将字符串从一个字符集转换为另一个字符集不同,引导器将字符串文字指定为具有特定的字符集,而不涉及转换。For more information, see Section 10.3.8, “Character Set Introducers”.有关更多信息,请参阅第10.3.8节,“字符集引导器”。
Character set conversion is also useful preceding lettercase conversion of binary strings. 字符集转换在二进制字符串的字母转换之前也很有用。LOWER()
and UPPER()
are ineffective when applied directly to binary strings because the concept of lettercase does not apply. LOWER()
和UPPER()
在直接应用于二进制字符串时是无效的,因为lettercase的概念不适用。To perform lettercase conversion of a binary string, first convert it to a nonbinary string using a character set appropriate for the data stored in the string:要执行二进制字符串的字母大小写转换,请首先使用适合字符串中存储的数据的字符集将其转换为非二进制字符串:
mysql>SET @str = BINARY 'New York';
mysql>SELECT LOWER(@str), LOWER(CONVERT(@str USING utf8mb4));
+-------------+------------------------------------+ | LOWER(@str) | LOWER(CONVERT(@str USING utf8mb4)) | +-------------+------------------------------------+ | New York | new york | +-------------+------------------------------------+
Be aware that if you convert an indexed column using 请注意,如果使用BINARY
, CAST()
, or CONVERT()
, MySQL may not be able to use the index efficiently.BINARY
、CAST()
或CONVERT()
转换索引列,MySQL可能无法有效地使用索引。
The cast functions are useful for creating a column with a specific type in a 在CREATE TABLE ... SELECT
statement:CREATE TABLE ... SELECT
语句中,若要创建具有特定类型的列,则CAST
函数很有用:
mysql>CREATE TABLE new_table SELECT CAST('2000-01-01' AS DATE) AS c1;
mysql>SHOW CREATE TABLE new_table\G
*************************** 1. row *************************** Table: new_table Create Table: CREATE TABLE `new_table` ( `c1` date DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
The cast functions are useful for sorting ENUM
columns in lexical order. CAST
函数用于按词法顺序对ENUM
列进行排序。Normally, sorting of 通常,使用内部数值对ENUM
columns occurs using the internal numeric values. ENUM
列进行排序。Casting the values to 将值强制转换为CHAR
results in a lexical sort:CHAR
将导致词汇排序:
SELECTenum_col
FROMtbl_name
ORDER BY CAST(enum_col
AS CHAR);
如果将其用作更复杂表达式(如CAST()
also changes the result if you use it as part of a more complex expression such as CONCAT('Date: ',CAST(NOW() AS DATE))
.CONCAT('Date: ',CAST(NOW() AS DATE))
)的一部分,CAST()
还会更改结果。
For temporal values, there is little need to use 对于时态值,几乎不需要使用CAST()
to extract data in different formats. CAST()
来提取不同格式的数据。Instead, use a function such as 相反,请使用诸如EXTRACT()
, DATE_FORMAT()
, or TIME_FORMAT()
. EXTRACT()
、DATE_FORMAT()
或TIME_FORMAT()
之类的函数。See Section 12.7, “Date and Time Functions”.请参阅第12.7节,“日期和时间函数”。
To cast a string to a number, it normally suffices to use the string value in numeric context:要将字符串转换为数字,通常在数字上下文中使用字符串值就足够了:
mysql> SELECT 1+'1';
-> 2
That is also true for hexadecimal and bit literals, which are binary strings by default:十六进制和位文字也是如此,默认情况下是二进制字符串:
mysql>SELECT X'41', X'41'+0;
-> 'A', 65 mysql>SELECT b'1100001', b'1100001'+0;
-> 'a', 97
A string used in an arithmetic operation is converted to a floating-point number during expression evaluation.算术运算中使用的字符串在表达式求值期间转换为浮点数。
A number used in string context is converted to a string:字符串上下文中使用的数字转换为字符串:
mysql> SELECT CONCAT('hello you ',2);
-> 'hello you 2'
For information about implicit conversion of numbers to strings, see Section 12.3, “Type Conversion in Expression Evaluation”.有关数字到字符串的隐式转换的信息,请参阅第12.3节,“表达式求值中的类型转换”。
MySQL supports arithmetic with both signed and unsigned 64-bit values. MySQL支持有符号和无符号64位值的算术。For numeric operators (such as 对于其中一个操作数是无符号整数的数字运算符(如+
or -
) where one of the operands is an unsigned integer, the result is unsigned by default (see Section 12.6.1, “Arithmetic Operators”). +
或-
),默认情况下结果是无符号的(请参阅第12.6.1节,“算术运算符”)。To override this, use the 若要重写此值,请使用SIGNED
or UNSIGNED
cast operator to cast a value to a signed or unsigned 64-bit integer, respectively.SIGNED
或UNSIGNED
强制转换运算符将值分别强制转换为有符号或无符号64位整数。
mysql>SELECT 1 - 2;
-> -1 mysql>SELECT CAST(1 - 2 AS UNSIGNED);
-> 18446744073709551615 mysql>SELECT CAST(CAST(1 - 2 AS UNSIGNED) AS SIGNED);
-> -1
If either operand is a floating-point value, the result is a floating-point value and is not affected by the preceding rule. 如果任一操作数是浮点值,则结果是浮点值,不受前面规则的影响。(In this context, (在此上下文中,DECIMAL
column values are regarded as floating-point values.)DECIMAL
列值被视为浮点值。)
mysql> SELECT CAST(1 AS UNSIGNED) - 2.0;
-> -1.0
The SQL mode affects the result of conversion operations (see Section 5.1.11, “Server SQL Modes”). SQL模式影响转换操作的结果(请参阅第5.1.11节,“服务器SQL模式”)。Examples:例如:
For conversion of a “zero” date string to a date, 要将“零”日期字符串转换为日期,CONVERT()
and CAST()
return NULL
and produce a warning when the NO_ZERO_DATE
SQL mode is enabled.CONVERT()
和CAST()
返回NULL
,并在启用NO_ZERO_DATE
SQL模式时生成警告。
For integer subtraction, if the 对于整数减法,如果启用了NO_UNSIGNED_SUBTRACTION
SQL mode is enabled, the subtraction result is signed even if any operand is unsigned.NO_UNSIGNED_SUBTRACTION
SQL模式,则即使任何操作数是无符号的,减法结果也是有符号的。
The following list describes the available cast functions and operators:下表描述了可用的强制转换函数和运算符:
BINARY
expr
The BINARY
operator converts the expression to a binary string (a string that has the binary
character set and binary
collation). BINARY
运算符将表达式转换为二进制字符串(具有二进制字符集和二进制排序规则的字符串)。A common use for BINARY
is to force a character string comparison to be done byte by byte using numeric byte values rather than character by character. BINARY
的一个常见用法是强制字符串比较是使用数字字节值逐字节进行的,而不是逐字符进行的。The BINARY
operator also causes trailing spaces in comparisons to be significant. BINARY
运算符还导致比较中的尾随空格非常重要。For information about the differences between the 有关binary
collation of the binary
character set and the _bin
collations of nonbinary character sets, see Section 10.8.5, “The binary Collation Compared to _bin Collations”.binary
字符集的_bin
排序规则与非二进制字符集的binary
排序规则之间差异的信息,请参阅第10.8.5节,“二进制排序规则与二进制排序规则的比较”。
mysql>SELECT 'a' = 'A';
-> 1 mysql>SELECT BINARY 'a' = 'A';
-> 0 mysql>SELECT 'a' = 'a ';
-> 1 mysql>SELECT BINARY 'a' = 'a ';
-> 0
In a comparison, 相比之下,BINARY
affects the entire operation; it can be given before either operand with the same result.BINARY
影响整个操作;可以在具有相同结果的任一操作数之前给定。
To convert a string expression to a binary string, these constructs are equivalent:要将字符串表达式转换为二进制字符串,以下构造是等效的:
CAST(expr
AS BINARY) CONVERT(expr
USING BINARY) BINARYexpr
If a value is a string literal, it can be designated as a binary string without converting it by using the 如果值是字符串文字,则可以将其指定为二进制字符串,而无需使用_binary
character set introducer:_binary
字符集引入器进行转换:
mysql>SELECT 'a' = 'A';
-> 1 mysql>SELECT _binary 'a' = 'A';
-> 0
For information about introducers, see Section 10.3.8, “Character Set Introducers”.有关引导器的信息,请参阅第10.3.8节,“字符集引导器”。
The 表达式中的BINARY运算符与字符列定义中的二进制属性在效果上有所不同。BINARY
operator in expressions differs in effect from the BINARY
attribute in character column definitions. For a character column defined with the 对于用BINARY
attribute, MySQL assigns the table default character set and the binary (_bin
) collation of that character set. BINARY
属性定义的字符列,MySQL分配表默认字符集和该字符集的二进制(_bin
)排序规则。Every nonbinary character set has a 每个非二进制字符集都有一个_bin
collation. _bin
排序规则。For example, if the table default character set is 例如,如果表的默认字符集是utf8mb4
, these two column definitions are equivalent:utf8mb4
,则这两个列定义是等效的:
CHAR(10) BINARY CHAR(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
The use of 在CHARACTER SET binary
in the definition of a CHAR
, VARCHAR
, or TEXT
column causes the column to be treated as the corresponding binary string data type. CHAR
、VARCHAR
或TEXT
列的定义中使用CHARACTER SET binary
会导致该列被视为相应的二进制字符串数据类型。For example, the following pairs of definitions are equivalent:例如,以下定义对是等效的:
CHAR(10) CHARACTER SET binary BINARY(10) VARCHAR(10) CHARACTER SET binary VARBINARY(10) TEXT CHARACTER SET binary BLOB
If 如果从mysql客户机中调用BINARY
is invoked from within the mysql client, binary strings display using hexadecimal notation, depending on the value of the --binary-as-hex
. BINARY
,则二进制字符串将使用十六进制表示法显示,具体取决于--binary-as-hex
的值。For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”.有关该选项的更多信息,请参阅第4.5.1节,“mysql命令行客户端”。
CAST(
timestamp_value
AT TIME ZONE timezone_specifier
AS DATETIME[(precision
)])
timezone_specifier
: [INTERVAL] '+00:00' | 'UTC'
The CAST()
function takes an expression of any type and produces a result value of the specified type, similar to CONVERT()
. CAST()
函数接受任何类型的表达式并生成指定类型的结果值,类似于CONVERT()
。For more information, see the description of 有关详细信息,请参见CONVERT()
.CONVERT()
的说明。
In MySQL 8.0.17 and higher, 在MySQL 8.0.17及更高版本中,InnoDB
allows the use of an additional ARRAY
keyword for creating a multi-valued index on a JSON
array as part of CREATE INDEX
, CREATE TABLE
, and ALTER TABLE
statements. InnoDB
允许使用额外的ARRAY
关键字在JSON
数组上创建多值索引,作为CREATE INDEX
、CREATE TABLE
和ALTER TABLE
语句的一部分。ARRAY
is not supported except when used to create a multi-valued index in one of these statements, in which case it is required. ARRAY
不受支持,除非用于在其中一个语句中创建多值索引,在这种情况下它是必需的。The column being indexed must be a column of type 被索引的列必须是JSON
. JSON
类型的列。With 对于ARRAY
, the type
following the AS
keyword may specify any of the types supported by CAST()
, with the exceptions of BINARY
, JSON
, and YEAR
. ARRAY
,AS
关键字后面的type
可以指定CAST()
支持的任何类型,但BINARY
、JSON
和YEAR
除外。For syntax information and examples, as well as other relevant information, see Multi-Valued Indexes.有关语法信息和示例以及其他相关信息,请参阅多值索引。
Unlike 与CAST()
, CONVERT()
does not support multi-valued index creation or the ARRAY
keyword.CAST()
不同,CONVERT()
不支持多值索引创建或数组关键字。
Beginning with MySQL 8.0.22, 从MySQL 8.0.22开始,CAST()
supports retrieval of a TIMESTAMP
value as being in UTC, using the AT TIMEZONE
operator. CAST()
支持使用AT TIMEZONE
操作符检索UTC格式的TIMESTAMP
值。The only supported time zone is UTC; this can be specified as either of 唯一支持的时区是UTC;可以指定为'+00:00'
or 'UTC'
. '+00:00'
或'UTC'
。The only return type supported by this syntax is 此语法支持的唯一返回类型是DATETIME
, with an optional precision specifier in the range of 0 to 6, inclusive.DATETIME
,其可选精度说明符的范围为0到6(包括0到6)。
还支持使用时区偏移量的TIMESTAMP
values that use timezone offsets are also supported.TIMESTAMP
值。
mysql>SELECT @@system_time_zone;
+--------------------+ | @@system_time_zone | +--------------------+ | EDT | +--------------------+ 1 row in set (0.00 sec) mysql>CREATE TABLE TZ (c TIMESTAMP);
Query OK, 0 rows affected (0.41 sec) mysql>INSERT INTO tz VALUES
>ROW(CURRENT_TIMESTAMP),
>ROW('2020-07-28 14:50:15+1:00');
Query OK, 1 row affected (0.08 sec) mysql>TABLE tz;
+---------------------+ | c | +---------------------+ | 2020-07-28 09:22:41 | | 2020-07-28 09:50:15 | +---------------------+ 2 rows in set (0.00 sec) mysql>SELECT CAST(c AT TIME ZONE '+00:00' AS DATETIME) AS u FROM tz;
+---------------------+ | u | +---------------------+ | 2020-07-28 13:22:41 | | 2020-07-28 13:50:15 | +---------------------+ 2 rows in set (0.00 sec) mysql>SELECT CAST(c AT TIME ZONE 'UTC' AS DATETIME(2)) AS u FROM tz;
+------------------------+ | u | +------------------------+ | 2020-07-28 13:22:41.00 | | 2020-07-28 13:50:15.00 | +------------------------+ 2 rows in set (0.00 sec)
If you use 如果您使用'UTC'
as the time zone specifier with this form of CAST()
, and the server raises an error such as Unknown or incorrect time zone: 'UTC', you may need to install the MySQL time zone tables (see Populating the Time Zone Tables).'UTC'
作为CAST()
形式的时区说明符,并且服务器引发未知或不正确的时区'UTC'等错误,则可能需要安装MySQL时区表(请参阅填充时区表)。
AT TIME ZONE
does not support the ARRAY
keyword, and is not supported by the CONVERT()
function.AT TIME ZONE
不支持ARRAY
关键字,CONVERT()
函数也不支持该关键字。
CONVERT(
, expr
USING transcoding_name
)CONVERT(
expr
,type
)
The CONVERT()
function takes an expression of any type and produces a result value of the specified type.CONVERT()
函数接受任何类型的表达式并生成指定类型的结果值。
CONVERT(... USING ...)
is standard SQL syntax. CONVERT(... USING ...)
是标准SQL语法。The non-USING
form of CONVERT()
is ODBC syntax.CONVERT()
的非USING
形式是ODBC语法。
CONVERT(
converts data between different character sets. expr
USING transcoding_name
)CONVERT(
在不同的字符集之间转换数据。expr
USING transcoding_name
)In MySQL, transcoding names are the same as the corresponding character set names. 在MySQL中,转码名称与对应的字符集名称相同。For example, this statement converts the string 例如,此语句将默认字符集中的字符串'abc'
in the default character set to the corresponding string in the utf8mb4
character set:'abc'
转换为utf8mb4
字符集中的相应字符串:
SELECT CONVERT('abc' USING utf8mb4);
CONVERT(
syntax (without expr
, type
)USING
) takes an expression and a type
value specifying the result type. CONVERT(
语法(不使用expr
, type
)USING
)接受表达式和指定结果类型的type
值。This operation may also be expressed as 此运算还可以表达为CAST(
, which is equivalent. expr
AS type
)CAST(
,它们是等同的。expr
AS type
)These 允许这些type
values are permitted:type
值:
BINARY[(
N
)]
Produces a string with the 生成BINARY
data type. BINARY
数据类型的字符串。For a description of how this affects comparisons, see Section 11.3.3, “The BINARY and VARBINARY Types”. 有关这如何影响比较的描述,请参阅第11.3.3节,“BINARY类型和VARBINARY类型”。If the optional length 如果给定了可选长度N
is given, BINARY(
causes the cast to use no more than N
)N
bytes of the argument. N
,BINARY(
将导致强制转换使用的参数不超过N
)N
个字节。Values shorter than 小于N
bytes are padded with 0x00
bytes to a length of N
.N
字节的值用0x00
字节填充,长度为N
。
CHAR[(
N
)] [charset_info
]
Produces a string with the 生成具有CHAR
data type. CHAR
数据类型的字符串。If the optional length 如果给定了可选长度N
is given, CHAR(
causes the cast to use no more than N
)N
characters of the argument. N
,CHAR(
将导致强制转换使用不超过N个字符的参数。N
)No padding occurs for values shorter than 小于N
characters.N
个字符的值不进行填充。
With no 如果不使用charset_info
clause, CHAR
produces a string with the default character set. charset_info
子句,CHAR
将生成具有默认字符集的字符串。To specify the character set explicitly, these 要显式指定字符集,允许使用以下charset_info
values are permitted:charset_info
值:
CHARACTER SET
charset_name
: Produces a string with the given character set.:生成具有给定字符集的字符串。
ASCII
: Shorthand for :CHARACTER SET latin1
.CHARACTER SET latin1
的缩写。
UNICODE
: Shorthand for :CHARACTER SET ucs2
.CHARACTER SET ucs2
的缩写。
In all cases, the string has the character set default collation.在所有情况下,字符串都具有字符集默认排序规则。
DATE
Produces a 生成DATE
value.DATE
值。
DATETIME
Produces a 生成DATETIME
value.DATETIME
值。
DECIMAL[(
M
[,D
])]
Produces a 生成DECIMAL
value. DECIMAL
值。If the optional 如果给出了可选的M
and D
values are given, they specify the maximum number of digits (the precision) and the number of digits following the decimal point (the scale).M
和D
值,它们将指定最大位数(精度)和小数点后的位数(刻度)。
DOUBLE
Produces a 产生DOUBLE
result. DOUBLE
结果。Added in MySQL 8.0.17.在MySQL 8.0.17中添加。
FLOAT[(
p
)]
If the precision 如果未指定精度p
is not specified, produces a result of type FLOAT
. p
,则生成FLOAT
类型的结果。If 如果提供p
is provided and 0 <= < p
<= 24, the result is of type FLOAT
. p
且0<=<p
<=24,结果为FLOAT
类型。If 25 <= 如果25<=p
<= 53, the result is of type DOUBLE
. p
<=53,结果是DOUBLE
类型。If 如果p
< 0 or p
> 53, an error is returned. p
<0或p
>53,返回错误。Added in MySQL 8.0.17.在MySQL 8.0.17中添加。
JSON
Produces a 生成JSON
value. JSON
值。For details on the rules for conversion of values between 有关JSON
and other types, see Comparison and Ordering of JSON Values.JSON
和其他类型之间的值转换规则的详细信息,请参阅JSON值的比较和排序。
NCHAR[(
N
)]
Like 与CHAR
, but produces a string with the national character set. CHAR
类似,但生成具有国家字符集的字符串。See Section 10.3.7, “The National Character Set”.见第10.3.7节,“国家字符集”。
Unlike 与CHAR
, NCHAR
does not permit trailing character set information to be specified.CHAR
不同,NCHAR
不允许指定尾随字符集信息。
REAL
Produces a result of type 生成类型为REAL
. REAL
的结果。This is actually 如果启用了FLOAT
if the REAL_AS_FLOAT
SQL mode is enabled; otherwise the result is of type DOUBLE
.REAL_AS_FLOAT
SQL模式,这实际上是FLOAT
;否则结果为DOUBLE
类型。
SIGNED [INTEGER]
Produces a signed integer value.生成有符号整数值。
spatial_type
As of MySQL 8.0.24, 从MySQL8.0.24开始,CAST()
and CONVERT()
support casting geometry values from one spatial type to another, for certain combinations of spatial types. CAST()
和CONVERT()
支持将几何体值从一种空间类型转换为另一种空间类型,用于空间类型的某些组合。The following list shows the permitted type combinations, where “MySQL extension” designates casts implemented in MySQL beyond those defined in the SQL/MM standard:下表显示了允许的类型组合,其中“MySQL extension”指定在MySQL中实现的强制转换,而不是SQL/MM标准中定义的强制转换:
From Point
to:
MultiPoint
GeometryCollection
From LineString
to:
Polygon
(MySQL extension)
MultiPoint
(MySQL extension)
MultiLineString
GeometryCollection
From Polygon
to:
LineString
(MySQL extension)
MultiLineString
(MySQL extension)
MultiPolygon
GeometryCollection
From MultiPoint
to:
Point
LineString
(MySQL extension)
GeometryCollection
From MultiLineString
to:
LineString
Polygon
(MySQL extension)
MultiPolygon
(MySQL extension)
GeometryCollection
From MultiPolygon
to:
Polygon
MultiLineString
(MySQL extension)
GeometryCollection
From GeometryCollection
to:
Point
LineString
Polygon
MultiPoint
MultiLineString
MultiPolygon
In spatial casts, 在空间强制转换中,GeometryCollection
and GeomCollection
are synonyms for the same result type.GeometryCollection
和GeomCollection
是同一结果类型的同义词。
Some conditions apply to all spatial type casts, and some conditions apply only when the cast result is to have a particular spatial type. 有些条件适用于所有空间类型强制转换,有些条件仅适用于强制转换结果具有特定空间类型的情况。For information about terms such as “well-formed geometry,” see Section 11.4.4, “Geometry Well-Formedness and Validity”.有关术语的信息,如“几何形状良好”,请参阅第11.4.4节,“几何形状良好性和有效性”。
These conditions apply to all spatial casts regardless of the result type:无论结果类型如何,这些条件都适用于所有空间强制转换:
The result of a cast is in the same SRS as that of the expression to cast.强制转换的结果与要强制转换的表达式的结果在相同的SRS中。
Casting between spatial types does not change coordinate values or order.在空间类型之间转换不会更改坐标值或顺序。
If the expression to cast is 如果要强制转换的表达式为NULL
, the function result is NULL
.NULL
,则函数结果为NULL
。
Casting to spatial types using the 不允许使用带有指定空间类型的返回子句的JSON_VALUE()
function with a RETURNING
clause specifying a spatial type is not permitted.JSON_VALUE()
函数强制转换为空间类型。
Casting to an 不允许强制转换为空间类型的ARRAY
of spatial types is not permitted.ARRAY
。
If the spatial type combination is permitted but the expression to cast is not a syntactically well-formed geometry, an 如果允许空间类型组合,但要强制转换的表达式不是语法格式良好的几何体,则会发生ER_GIS_INVALID_DATA
error occurs.ER_GIS_INVALID_DATA
错误。
If the spatial type combination is permitted but the expression to cast is a syntactically well-formed geometry in an undefined spatial reference system (SRS), an 如果允许空间类型组合,但要强制转换的表达式是未定义空间参照系(SRS)中语法形式良好的几何体,则会发生ER_SRS_NOT_FOUND
error occurs.ER_SRS_NOT_FOUND
错误。
If the expression to cast has a geographic SRS but has a longitude or latitude that is out of range, an error occurs:如果要强制转换的表达式具有地理SRS,但其经度或纬度超出范围,则会发生错误:
If a longitude value is not in the range (−180, 180], an 如果经度值不在范围(-180,180)内,则会发生ER_GEOMETRY_PARAM_LONGITUDE_OUT_OF_RANGE
error occurs.ER_GEOMETRY_PARAM_LONGITUDE_OUT_OF_RANGE
错误。
If a latitude value is not in the range [−90, 90], an 如果纬度值不在[-90,90]范围内,则会发生ER_GEOMETRY_PARAM_LATITUDE_OUT_OF_RANGE
error occurs.ER_GEOMETRY_PARAM_LATITUDE_OUT_OF_RANGE
错误。
Ranges shown are in degrees. 显示的范围以度为单位。If an SRS uses another unit, the range uses the corresponding values in its unit. 如果SRS使用另一个单位,则范围使用其单位中的相应值。The exact range limits deviate slightly due to floating-point arithmetic.由于采用浮点运算,精确的范围限制略有偏差。
These conditions apply when the cast result type is 当强制转换结果类型为Point
:Point
时,这些条件适用:
If the expression to cast is a well-formed geometry of type 如果要转换的表达式是Point
, the function result is that Point
.Point
类型的格式良好的几何体,则函数结果就是该Point
。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是包含单个MultiPoint
containing a single Point
, the function result is that Point
. Point
的格式良好的MultiPoint
类型几何体,则函数结果就是该Point
。If the expression contains more than one 如果表达式包含多个点,则会发生Point
, an ER_INVALID_CAST_TO_GEOMETRY
error occurs.ER_INVALID_CAST_TO_GEOMETRY
错误。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是GeometryCollection
containing only a single Point
, the function result is that Point
. GeometryCollection
类型的格式良好的几何体,且仅包含一个点,则函数结果就是该点。If the expression is empty, contains more than one 如果表达式为空、包含多个Point
, or contains other geometry types, an ER_INVALID_CAST_TO_GEOMETRY
error occurs.Point
或包含其他几何图形类型,则会发生ER_INVALID_CAST_TO_GEOMETRY
错误。
If the expression to cast is a well-formed geometry of type other than 如果要强制转换的表达式是一个格式良好的几何体,而不是Point
, MultiPoint
, GeometryCollection
, an ER_INVALID_CAST_TO_GEOMETRY
error occurs.Point
、MultiPoint
、GeometryCollection
类型,则会发生ER_INVALID_CAST_TO_GEOMETRY
错误。
These conditions apply when the cast result type is 当强制转换结果类型为LineString
:LineString
时,这些条件适用:
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是LineString
, the function result is that LineString
.LineString
类型的格式良好的几何体,则函数结果为该LineString
。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是没有内环的Polygon
that has no inner rings, the function result is a LineString
containing the points of the outer ring in the same order. Polygon
类型的格式良好的几何体,则函数结果是一个以相同顺序包含外环点的线字符串。If the expression has inner rings, an 如果表达式有内环,则会发生ER_INVALID_CAST_TO_GEOMETRY
error occurs.ER_INVALID_CAST_TO_GEOMETRY
错误。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是一个格式良好的MultiPoint
containing at least two points, the function result is a LineString
containing the points of the MultiPoint
in the order they appear in the expression. MultiPoint
类型几何体,其中至少包含两个点,则函数结果是一个按多点在表达式中出现的顺序包含这些点的线字符串。If the expression contains only one 如果表达式仅包含一个Point
, an ER_INVALID_CAST_TO_GEOMETRY
error occurs.Point
,则会发生ER_INVALID_CAST_TO_GEOMETRY
错误。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是一个格式良好的MultiLineString
containing a single LineString
, the function result is that LineString
. MultiLineString
类型的几何体,其中包含一个LineString
,则函数结果为该LineString
。If the expression contains more than one 如果表达式包含多个LineString
, an ER_INVALID_CAST_TO_GEOMETRY
error occurs.LineString
,则会发生ER_INVALID_CAST_TO_GEOMETRY
错误。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是GeometryCollection
, containing only a single LineString
, the function result is that LineString
. GeometryCollection
类型的格式良好的几何体,并且只包含一个LineString
,则函数结果就是该LineString
。If the expression is empty, contains more than one 如果表达式为空、包含多个LineString
, or contains other geometry types, an ER_INVALID_CAST_TO_GEOMETRY
error occurs.LineString
或包含其他几何图形类型,则会发生ER_INVALID_CAST_TO_GEOMETRY
错误。
If the expression to cast is a well-formed geometry of type other than 如果要强制转换的表达式是LineString
, Polygon
, MultiPoint
, MultiLineString
, or GeometryCollection
, an ER_INVALID_CAST_TO_GEOMETRY
error occurs.LineString
、Polygon
、MultiPoint
、MultiLineString
或GeometryCollection
以外类型的格式良好的几何体,则会发生ER_INVALID_CAST_TO_GEOMETRY
错误。
These conditions apply when the cast result type is 当“强制转换结果类型”为Polygon
:Polygon
时,这些条件适用:
If the expression to cast is a well-formed geometry of type 如果要转换的表达式是LineString
that is a ring (that is, the start and end points are the same), the function result is a Polygon
with an outer ring consisting of the points of the LineString
in the same order. LineString
类型的格式良好的几何体(即起点和终点相同),则函数结果是一个Polygon
,其外圈由LineString
的点按相同顺序组成。If the expression is not a ring, an 如果表达式不是环,则会发生ER_INVALID_CAST_TO_GEOMETRY
error occurs. ER_INVALID_CAST_TO_GEOMETRY
错误。If the ring is not in the correct order (the exterior ring must be counter-clockwise), an 如果环的顺序不正确(外环必须是逆时针方向),则会出现ER_INVALID_CAST_POLYGON_RING_DIRECTION
error occurs.ER_INVALID_CAST_POLYGON_RING_DIRECTION
错误。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是Polygon
, the function result is that Polygon
.Polygon
类型的格式良好的几何体,则函数结果是该Polygon
。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是一个格式良好的MultiLineString
where all elements are rings, the function result is a Polygon
with the first LineString
as outer ring and any additional LineString
values as inner rings. MultiLineString
类型的几何体,其中所有元素都是环,则函数结果是一个Polygon
,第一个LineString
作为外圈,任何其他LineString
值作为内圈。If any element of the expression is not a ring, an 如果表达式的任何元素不是环,则会发生ER_INVALID_CAST_TO_GEOMETRY
error occurs. ER_INVALID_CAST_TO_GEOMETRY
错误。If any ring is not in the correct order (the exterior ring must be counter-clockwise, interior rings must be clockwise), an 如果任何环的顺序不正确(外圈必须为逆时针方向,内圈必须为顺时针方向),则会出现ER_INVALID_CAST_POLYGON_RING_DIRECTION
error occurs.ER_INVALID_CAST_POLYGON_RING_DIRECTION
错误。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是包含单个MultiPolygon
containing a single Polygon
, the function result is that Polygon
. Polygon
的格式良好的MultiPolygon
类型几何体,则函数结果为该Polygon
。If the expression contains more than one 如果表达式包含多个Polygon
, an ER_INVALID_CAST_TO_GEOMETRY
error occurs.Polygon
,则会发生ER_INVALID_CAST_TO_GEOMETRY
错误。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是GeometryCollection
containing only a single Polygon
, the function result is that Polygon
. GeometryCollection
类型的格式良好的几何体,且仅包含一个Polygon
,则函数结果为该Polygon
。If the expression is empty, contains more than one 如果表达式为空、包含多个Polygon
, or contains other geometry types, an ER_INVALID_CAST_TO_GEOMETRY
error occurs.Polygon
或包含其他几何体类型,则会发生ER_INVALID_CAST_TO_GEOMETRY
错误。
If the expression to cast is a well-formed geometry of type other than 如果要强制转换的表达式是LineString
, Polygon
, MultiLineString
, MultiPolygon
, or GeometryCollection
, an ER_INVALID_CAST_TO_GEOMETRY
error occurs.LineString
、Polygon
、MultiLineString
、MultiPolygon
或GeometryCollection
以外类型的格式良好的几何体,则会发生ER_INVALID_CAST_TO_GEOMETRY
错误。
These conditions apply when the cast result type is 当强制转换结果类型为MultiPoint
:MultiPoint
时,这些条件适用:
If the expression to cast is a well-formed geometry of type 如果要转换的表达式是Point
, the function result is a MultiPoint
containing that Point
as its sole element.Point
类型的格式良好的几何体,则函数结果是一个MultiPoint
,其中包含该Point
作为其唯一元素。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是LineString
, the function result is a MultiPoint
containing the points of the LineString
in the same order.LineString
类型的格式良好的几何体,则函数结果是一个MultiPoint
,其中包含LineString
中按相同顺序排列的点。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是格式良好的MultiPoint
, the function result is that MultiPoint
.MultiPoint
类型几何体,则函数结果就是该MultiPoint
。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是仅包含点的GeometryCollection
containing only points, the function result is a MultiPoint
containing those points. GeometryCollection
类型的格式良好的几何体,则函数结果是包含这些点的MultiPoint
。If the 如果GeometryCollection
is empty or contains other geometry types, an ER_INVALID_CAST_TO_GEOMETRY
error occurs.GeometryCollection
为空或包含其他几何图形类型,则会发生ER_INVALID_CAST_TO_GEOMETRY
错误。
If the expression to cast is a well-formed geometry of type other than 如果要强制转换的表达式是一个格式良好的几何图形,而不是Point
, LineString
, MultiPoint
, or GeometryCollection
, an ER_INVALID_CAST_TO_GEOMETRY
error occurs.Point
、LineString
、MultiPoint
或GeometryCollection
类型,则会发生ER_INVALID_CAST_TO_GEOMETRY
错误。
These conditions apply when the cast result type is 当强制转换结果类型为MultiLineString
:MultiLineString
时,这些条件适用:
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是LineString
, the function result is a MultiLineString
containing that LineString
as its sole element.LineString
类型的格式良好的几何体,则函数结果是包含该LineString
作为其唯一元素的MultiLineString
。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是Polygon
, the function result is a MultiLineString
containing the outer ring of the Polygon
as its first element and any inner rings as additional elements in the order they appear in the expression.Polygon
类型的格式良好的几何体,则函数结果是一个MultiLineString
,其中包含Polygon
的外圈作为其第一个元素,任何内圈按照它们在表达式中出现的顺序作为附加元素。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是格式良好的MultiLineString
, the function result is that MultiLineString
.MultiLineString
类型的几何体,则函数结果是该MultiLineString
。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是一个格式良好的MultiPolygon
containing only polygons without inner rings, the function result is a MultiLineString
containing the polygon rings in the order they appear in the expression. MultiPolygon
类型的几何体,它只包含没有内环的多边形,则函数结果是一个按多边形环在表达式中出现的顺序包含多边形环的MultiLineString
。If the expression contains any polygons with inner rings, an 如果表达式包含任何带内环的ER_WRONG_PARAMETERS_TO_STORED_FCT
error occurs.Polygon
,则会发生ER_WRONG_PARAMETERS_TO_STORED_FCT
错误。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是GeometryCollection
containing only linestrings, the function result is a MultiLineString
containing those linestrings. GeometryCollection
类型的格式良好的几何体,且仅包含线字符串,则函数结果是包含这些线字符串的多重线字符串。If the expression is empty or contains other geometry types, an 如果表达式为空或包含其他几何体类型,则会发生ER_INVALID_CAST_TO_GEOMETRY
error occurs.ER_INVALID_CAST_TO_GEOMETRY
错误。
If the expression to cast is a well-formed geometry of type other than 如果要强制转换的表达式是LineString
, Polygon
, MultiLineString
, MultiPolygon
, or GeometryCollection
, an ER_INVALID_CAST_TO_GEOMETRY
error occurs.LineString
、Polygon
、MultiLineString
、MultiPolygon
或GeometryCollection
以外类型的格式良好的几何体,则会发生ER_INVALID_CAST_TO_GEOMETRY
错误。
These conditions apply when the cast result type is 当强制转换结果类型为MultiPolygon
:MultiPolygon
时,这些条件适用:
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是Polygon
, the function result is a MultiPolygon
containing the Polygon
as its sole element.Polygon
类型的格式良好的几何体,则函数结果是包含Polygon
作为其唯一元素的MultiPolygon
。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是一个格式良好的MultiLineString
where all elements are rings, the function result is a MultiPolygon
containing a Polygon
with only an outer ring for each element of the expression. MultiLineString
类型的几何体,其中所有元素都是环,则函数结果是一个包含Polygon
的MultiPolygon
,表达式的每个元素只有一个外圈。If any element is not a ring, an 如果任一元素不是环,则会发生ER_INVALID_CAST_TO_GEOMETRY
error occurs. ER_INVALID_CAST_TO_GEOMETRY
错误。If any ring is not in the correct order (exterior ring must be counter-clockwise), an 如果任何环的顺序不正确(外环必须逆时针),则会出现ER_INVALID_CAST_POLYGON_RING_DIRECTION
error occurs.ER_INVALID_CAST_POLYGON_RING_DIRECTION
错误。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是格式良好的MultiPolygon
, the function result is that MultiPolygon
.MultiPolygon
类型的几何体,则函数结果是该MultiPolygon
。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是仅包含多边形的GeometryCollection
containing only polygons, the function result is a MultiPolygon
containing those polygons. GeometryCollection
类型的格式良好的几何体,则函数结果是包含这些多边形的MultiPolygon
。If the expression is empty or contains other geometry types, an 如果表达式为空或包含其他几何体类型,则会发生ER_INVALID_CAST_TO_GEOMETRY
error occurs.ER_INVALID_CAST_TO_GEOMETRY
错误。
If the expression to cast is a well-formed geometry of type other than 如果要强制转换的表达式是Polygon
, MultiLineString
, MultiPolygon
, or GeometryCollection
, an ER_INVALID_CAST_TO_GEOMETRY
error occurs.Polygon
、MultiLineString
、MultiPolygon
或GeometryCollection
以外类型的格式良好的几何体,则会发生ER_INVALID_CAST_TO_GEOMETRY
错误。
These conditions apply when the cast result type is 当强制转换结果类型为GeometryCollection
:GeometryCollection
时,这些条件适用:
GeometryCollection
and GeomCollection
are synonyms for the same result type.GeometryCollection
和GeometryCollection
是同一结果类型的同义词。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是Point
, the function result is a GeometryCollection
containing that Point
as its sole element.Point
类型的格式良好的几何体,则函数结果是一个GeometryCollection
,其中包含该Point
作为其唯一元素。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是LineString
, the function result is a GeometryCollection
containing that LineString
as its sole element.LineString
类型的格式良好的几何体,则函数结果是一个GeometryCollection
,其中包含该LineString
作为其唯一元素。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是Polygon
, the function result is a GeometryCollection
containing that Polygon
as its sole element.Polygon
类型的格式良好的几何体,则函数结果是包含该Polygon
作为其唯一元素的GeometryCollection
。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是类型为MultiPoint
, the function result is a GeometryCollection
containing the points in the order they appear in the expression.MultiPoint
的格式良好的几何体,则函数结果是一个GeometryCollection
,其中按点在表达式中出现的顺序包含这些点。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是格式良好的MultiLineString
, the function result is a GeometryCollection
containing the linestrings in the order they appear in the expression.MultiLineString
类型的几何体,则函数结果是一个GeometryCollection
,其中按线串在表达式中的显示顺序包含线串。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是格式良好的MultiPolygon
, the function result is a GeometryCollection
containing the elements of the MultiPolygon
in the order they appear in the expression.MultiPolygon
类型的几何体,则函数结果是一个GeometryCollection
,该集合按MultiPolygon
元素在表达式中出现的顺序包含这些元素。
If the expression to cast is a well-formed geometry of type 如果要强制转换的表达式是GeometryCollection
, the function result is that GeometryCollection
.GeometryCollection
类型的格式良好的几何体,则函数结果为该GeometryCollection
。
TIME
Produces a 生成TIME
value.TIME
值。
UNSIGNED [INTEGER]
Produces an unsigned integer value.生成无符号整数值。
YEAR
Produces a 生成YEAR
value. YEAR
值。Added in MySQL 8.0.22. These rules govern conversion to 在MySQL 8.0.22中添加。这些规则适用于转换为YEAR
:YEAR
:
For a four-digit number in the range 1901-2155 inclusive, or for a string which can be interpreted as a four-digit number in this range, return the corresponding 对于1901-2155(含1901-2155)范围内的四位数字,或对于可解释为该范围内四位数字的字符串,返回相应的YEAR
value.YEAR
值。
For a number consisting of one or two digits, or for a string which can be interpeted as such a number, return a 对于由一个或两个数字组成的数字,或可以解释为这样一个数字的字符串,按如下方式返回YEAR
value as follows:YEAR
值:
If the number is in the range 1-69 inclusive, add 2000 and return the sum.如果数字在1-69之间(含1-69),则加2000并返回总和。
If the number is in the range 70-99 inclusive, add 1900 and return the sum.如果数字在70-99之间(含70-99),则加1900并返回总和。
For a string which evaluates to 0, return 2000.对于计算结果为0的字符串,返回2000。
For the number 0, return 0.对于数字0,返回0。
For a 对于DATE
, DATETIME
, or TIMESTAMP
value, return the YEAR
portion of the value. DATE
、DATETIME
或TIMESTAMP
值,返回值的年份部分。For a 对于TIME
value, return the current year.TIME
值,返回当前年份。
If you do not specify the type of a 如果不指定TIME
argument, you may get a different result from what you expect, as shown here:TIME
参数的类型,可能会得到与预期不同的结果,如下所示:
mysql> SELECT CONVERT("11:35:00", YEAR), CONVERT(TIME "11:35:00", YEAR);
+---------------------------+--------------------------------+
| CONVERT("11:35:00", YEAR) | CONVERT(TIME "11:35:00", YEAR) |
+---------------------------+--------------------------------+
| 2011 | 2020 |
+---------------------------+--------------------------------+
If the argument is of type 如果参数的类型为DECIMAL
, DOUBLE
, DECIMAL
, or REAL
, round the value to the nearest integer, then attempt to cast the value to YEAR
using the rules for integer values, as shown here:DECIMAL
、DOUBLE
、DECIMAL
或REAL
,请将值舍入到最接近的整数,然后尝试使用整数值规则将值强制转换为YEAR
,如下所示:
mysql>SELECT CONVERT(1944.35, YEAR), CONVERT(1944.50, YEAR);
+------------------------+------------------------+ | CONVERT(1944.35, YEAR) | CONVERT(1944.50, YEAR) | +------------------------+------------------------+ | 1944 | 1945 | +------------------------+------------------------+ 1 row in set (0.00 sec) mysql>SELECT CONVERT(66.35, YEAR), CONVERT(66.50, YEAR);
+----------------------+----------------------+ | CONVERT(66.35, YEAR) | CONVERT(66.50, YEAR) | +----------------------+----------------------+ | 2066 | 2067 | +----------------------+----------------------+ 1 row in set (0.00 sec)
An argument of type 类型为GEOMETRY
cannot be converted to YEAR
.GEOMETRY
的参数不能转换为YEAR
。
For a value that cannot be successfully converted to 对于无法成功转换为YEAR
, return NULL
.YEAR
的值,返回NULL
。
A string value containing non-numeric characters which must be truncated prior to conversion raises a warning, as shown here:包含非数字字符的字符串值在转换之前必须被截断,将引发警告,如下所示:
mysql>SELECT CONVERT("1979aaa", YEAR);
+--------------------------+ | CONVERT("1979aaa", YEAR) | +--------------------------+ | 1979 | +--------------------------+ 1 row in set, 1 warning (0.00 sec) mysql>SHOW WARNINGS;
+---------+------+-------------------------------------------+ | Level | Code | Message | +---------+------+-------------------------------------------+ | Warning | 1292 | Truncated incorrect YEAR value: '1979aaa' | +---------+------+-------------------------------------------+