Example
Convert a value to the DATE
data type.
SELECT CAST(“2017-08-29” AS DATE); |
Definition and Usage
The CAST() function converts a value of any type into the specified data type.
Tip: You may also consider using the CONVERT() function for similar conversions.
Syntax
Parameter Values
Parameter
|
Description
|
value
|
Required: The value that will be converted to the specified data type.
|
datatype
|
Required: The data type to which the value will be converted, which can be any of the following options:
value
|
description
|
DATE
|
Transforms a value into a DATE format: “YYYY-MM-DD”.
|
DATETIME
|
Transforms a value into the DATETIME format: “YYYY-MM-DD HH:MM
“.
|
DECIMAL
|
Converts a value to a DECIMAL format, allowing specification of the maximum number of digits (M) and the number of digits after the decimal point (D).
|
TIME
|
Transforms a value into the TIME format: “HH:MM
“.
|
CHAR
|
Converts a value into a fixed-length string format (CHAR).
|
NCHAR
|
Converts a value into an NCHAR format, which produces a string using the national character set.
|
SIGNED
|
Converts a value into a SIGNED format, specifically a signed 64-bit integer.
|
UNSIGNED
|
Converts a value into an UNSIGNED format, specifically an unsigned 64-bit integer.
|
BINARY
|
Converts a value into a BINARY format, representing it as a binary string.
|
|
Technical Details
Works in:
|
From MySQL version 4.0
|
More Examples
Example
Converts a value into the CHAR datatype.
SELECT CAST(150 AS CHAR); |
Example
Converts a value into the TIME datatype.
SELECT CAST(“14:06:10” AS TIME); |
Example
Converts a value to a datatype that is signed.
SELECT CAST(5–10 AS SIGNED); |