MySQL CAST() Function
THE WORLD'S LARGEST WEB DEVELOPER SITE

MySQL CAST() Function

❮ MySQL Functions

Example

Convert a value from one datatype to another datatype:

SELECT CAST("2017-08-29" AS DATE);
Try it Yourself »

Definition and Usage

The CAST() function converts a value from one datatype to another datatype.

Tip: See also the CONVERT() function.

Syntax

CAST(value AS type)

Parameter Values

Parameter Description
value Required. The value to convert
type Required. The datatype to convert to. Can be one of the following:
Value Description
DATE Converts value to DATE type. Format is "YYYY-MM-DD" (Supported range is from "1000-01-01" to "9999-12-31")
DATETIME Converts value to DATETIME type. Format is "YYYY-MM-DD HH:MM:SS" (Supported range is from "1000-01-01 00:00:00" to "9999-12-31 23:59:59")
TIME Converts value to TIME type. Format is "HH:MM:SS" (Supported range is from "-838:59:59" to "838:59:59")
CHAR Converts value to CHAR type, which is a fixed length string
SIGNED Converts value to SIGNED type, which is a signed 64-bit integer
UNSIGNED Converts value to UNSIGNED type, which is an unsigned 64-bit integer
BINARY Converts value to BINARY type, which is a binary string

Note

  • CAST(value AS BINARY) is equivalent to using the BINARY function.

Technical Details

Works in: MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0.2

More Examples

Example

Convert a value from one datatype to another datatype:

SELECT CAST(150 AS CHAR);
Try it Yourself »

Example

Convert a value from one datatype to another datatype:

SELECT CAST("14:06:10" AS TIME);
Try it Yourself »

Example

Convert a value from one datatype to another datatype:

SELECT CAST(5-10 AS SIGNED);
Try it Yourself »

❮ MySQL Functions