目录

MySQL CAST() 函数

示例

将值转换为 DATE 数据类型:

SELECT CAST("2017-08-29" AS DATE);
亲自试一试 »

定义和用法

CAST() 函数将值(任何类型)转换为指定的数据类型。

提示:另请参阅转变()功能。

语法

CAST( value AS datatype)

参数值

Parameter Description
value Required. The value to convert
datatype Required. The datatype to convert to. Can be one of the following:
Value Description
DATE Converts value to DATE. Format: "YYYY-MM-DD"
DATETIME Converts value to DATETIME. Format: "YYYY-MM-DD HH:MM:SS"
DECIMAL Converts value to DECIMAL. Use the optional M and D parameters to specify the maximum number of digits (M) and the number of digits following the decimal point (D).
TIME Converts value to TIME. Format: "HH:MM:SS"
CHAR Converts value to CHAR (a fixed length string)
NCHAR Converts value to NCHAR (like CHAR, but produces a string with the national character set)
SIGNED Converts value to SIGNED (a signed 64-bit integer)
UNSIGNED Converts value to UNSIGNED (an unsigned 64-bit integer)
BINARY Converts value to BINARY (a binary string)

技术细节

工作于: 从 MySQL 4.0 开始

更多示例

示例

将值转换为 CHAR 数据类型:

SELECT CAST(150 AS CHAR);
亲自试一试 »

示例

将值转换为 TIME 数据类型:

SELECT CAST("14:06:10" AS TIME);
亲自试一试 »

示例

将值转换为 SIGNED 数据类型:

SELECT CAST(5-10 AS SIGNED);
亲自试一试 »