MySQL CAST() Function
In MySQL, the CAST() function converts the parameter value of any type to a value of the specified type and return the result.
CAST() Syntax
Here is the syntax of the MySQL CAST() function:
CAST(expr AS data_type)
CAST(expr AS data_type) is equivalent to CONVERT(expr, type).
Parameters
-
exprRequired. The value to be converted to a specified data type.
-
data_typeRequired. The target data type. You can use the following data types:
-
BINARY[(N)]: If the argument is empty (zero length), the result is ofBINARY(0)data type, otherwise the result is a string ofVARBINARYdata type. -
CHAR[(N)]: The result is a string ofVARCHARdata type. Unless the argument is empty, the result isCHAR(0)data type. -
DATE: The result is ofDATEdata type. -
DATETIME[(M)]: The result is ofDATETIMEdata type andMis the number of fractional seconds. -
DECIMAL[(M[,D])]: The result is ofDECIMALdata type. -
DOUBLE: The result is ofDOUBLEdata type. It was added in MySQL 8.0.17. -
FLOAT[(p)]: Convert the data according to the following rules- If not specified
p, the result is ofFLOATdata type. - If
0 <= p <= 24, the result is ofFLOATdata type. - If
25 <= p <= 53, the result is ofDOUBLEdata type. - If
p < 0orp > 53, return an error.
- If not specified
-
JSON: The result is ofJSONdata type. -
NCHAR[(N)]: The result is ofNCHARdata type. -
REAL: The result is ofREALdata type. Actually, it isFLOATifREAL_AS_FLOATenabled, otherwise it isDOUBLE. -
SIGNED [INTEGER]: The result is a signedBIGINTdata type. -
TIME[(M)]: The result is ofTIMEdata type andMis the number of fractional seconds. -
UNSIGNED [INTEGER]: The result is an unsignedBIGINTdata type. -
YEAR: The result is ofYEARdata type, which was added in MySQL 8.0.22.
-
Return value
The MySQL CAST() function converts a value of any data type to the specified data type and returns the result.
CAST() Examples
Here are some examples of CAST().
convert to binary
You can use the CAST() function to convert a regular string to a binary string.
SELECT CAST('hello' AS BINARY);
+--------------------------------------------------+
| CAST('hello' AS BINARY) |
+--------------------------------------------------+
| 0x68656C6C6F |
+--------------------------------------------------+Note that in the mysql client, binary strings are printed in hexadecimal by default.
You can also use the BINARY operator to get the binary string of a regular string as follow:
SELECT BINARY 'hello';
+--------------------------------+
| BINARY 'hello' |
+--------------------------------+
| 0x68656C6C6F |
+--------------------------------+Convert to JSON
You can use the CAST() function to convert a JSON document represented as a string to value of JSON data type.
SELECT CAST('[1, 2, "a"]' AS JSON);
+-----------------------------+
| CAST('[1, 2, "a"]' AS JSON) |
+-----------------------------+
| [1, 2, "a"] |
+-----------------------------+Convert to datetime
You can use the CAST() function to convert date/time values represented by strings to data of DATE, DATETIME type.
SELECT
CAST('2022-02-28' AS DATE),
CAST('10:10:10' AS TIME),
CAST('2022-02-28 10:10:10' AS DATETIME);
+----------------------------+--------------------------+-----------------------------------------+
| CAST('2022-02-28' AS DATE) | CAST('10:10:10' AS TIME) | CAST('2022-02-28 10:10:10' AS DATETIME) |
+----------------------------+--------------------------+-----------------------------------------+
| 2022-02-28 | 10:10:10 | 2022-02-28 10:10:10 |
+----------------------------+--------------------------+------------------------------------------