CAST
Syntax
Description
The CAST()
function takes a value of one type and produces a value of another type, similar to the CONVERT() function.
The type can be one of the following values:
[DECIMAL(M[,D])]
FLOAT (from MariaDB 10.4.5)
Short for
SIGNED INTEGER
SIGNED [INTEGER]
UNSIGNED [INTEGER]
VARCHAR (in Oracle mode, from MariaDB 10.3)
The main difference between CAST
and CONVERT() is that [CONVERT(expr,type)](convert.md)
is ODBC syntax while CAST(expr as type)
and [CONVERT(... USING ...)](convert.md)
are SQL92 syntax.
In MariaDB 10.4 and later, you can use the CAST()
function with the INTERVAL
keyword.
Until MariaDB 5.5.31, X'HHHH'
, the standard SQL syntax for binary string literals, erroneously worked in the same way as 0xHHHH
. In 5.5.31 it was intentionally changed to behave as a string in all contexts (and never as a number).
This introduced an incompatibility with previous versions of MariaDB, and all versions of MySQL (see the example below).
Examples
Simple casts:
Note that when one casts to CHAR without specifying the character set, the collation_connection character set collation will be used. When used with CHAR CHARACTER SET
, the default collation for that character set will be used.
If you also want to change the collation, you have to use the COLLATE
operator:
Using CAST()
to order an [ENUM](../../../../data-types/string-data-types/enum.md)
field as a [CHAR](../../../../data-types/string-data-types/char.md)
rather than the internal numerical value:
From MariaDB 5.5.31, the following will trigger warnings, since x'aa'
and 'X'aa'
no longer behave as a number. Previously, and in all versions of MySQL, no warnings are triggered since they did erroneously behave as a number:
Casting to intervals:
See Also
This page is licensed: GPLv2, originally from fill_help_tables.sql
Last updated
Was this helpful?