MariaDB CAST() Function
In MariaDB, BENCHMARK()
is a built-in function that converts a given value to the specified type and returns it.
MariaDB CAST()
syntax
Here is the syntax of the MariaDB CAST()
function:
CAST(expr AS data_type)
CAST(expr AS data_type)
is equivalent to CONVERT(expr, type)
.
Parameters
expr
-
Optional. A value whose data type needs to be converted.
data_type
-
Optional. The target data type. You can use the following types:
BINARY
CHAR
DATE
DATETIME
DECIMAL[(M[,D])]
DOUBLE
FLOAT
INTEGER
SIGNED [INTEGER]
UNSIGNED [INTEGER]
TIME
VARCHAR
Return value
The MariaDB CAST()
function converts the value of any data type to the specified data type and returns it.
MariaDB CAST()
Examples
The following example shows the usages of the MariaDB CAST()
function:
Convert to binary
You can use the CAST()
function to convert a string to a binary string.
SELECT CAST('hello' AS BINARY);
Output:
+-------------------------+
| CAST('hello' AS BINARY) |
+-------------------------+
| hello |
+-------------------------+
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 string, as follows:
SELECT BINARY 'hello';
Output:
+----------------+
| BINARY 'hello' |
+----------------+
| hello |
+----------------+
Convert to integer
You can use the CAST()
function to convert a floating point number to an integer.
SELECT
CAST(1.23 AS INT),
CAST('1.23' AS INT),
CAST('1.23A' AS INT);
Output:
+-------------------+---------------------+----------------------+
| CAST(1.23 AS INT) | CAST('1.23' AS INT) | CAST('1.23A' AS INT) |
+-------------------+---------------------+----------------------+
| 1 | 1 | 1 |
+-------------------+---------------------+----------------------+
MariaDB will return 0
if MariaDB cannot convert the given value to an integer.
SELECT CAST('abc' AS INT);
Output:
+--------------------+
| CAST('abc' AS INT) |
+--------------------+
| 0 |
+--------------------+
Convert to datetime
You can use the CAST()
function to convert a date/time value represented by a string to a value of DATE
or DATETIME
type .
SELECT
CAST('2022-02-28' AS DATE),
CAST('10:10:10' AS TIME),
CAST('2022-02-28 10:10:10' AS DATETIME);
Output:
+----------------------------+--------------------------+-----------------------------------------+
| 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 |
+----------------------------+--------------------------+-----------------------------------------+
MariaDB will return NULL
if MariaDB cannot convert the given value to a date/time type.
SELECT CAST('ABC' AS DATE);
Output:
+---------------------+
| CAST('ABC' AS DATE) |
+---------------------+
| NULL |
+---------------------+
Conclusion
In MariaDB, BENCHMARK()
is a built-in function that converts a given value to the specified type and returns it.