MariaDB CAST() Function
The MariaDB CAST() function converts a value from one datatype to another datatype.
Syntax
CAST(value AS type)
Parameters
value |
Required. Specify the value to convert. | ||||||||||||||||||||
type |
Required. Specify the datatype to convert to. It can be one of the following:
|
Return Value
Returns the converted value.
Convert to DATE
The CAST() function can be used to convert a value to a DATE type. For example - in the example below, '2018-08-18' is converted to DATE datatype.
SELECT CAST('2018-08-18' AS DATE); Result: '2018-08-31'
Convert to DATETIME
The CAST() function can be used to convert a value to a DATETIME type. For example - in the example below, '2018-08-18 10:38:42' is converted to DATETIME datatype.
SELECT CAST('2018-08-18 10:38:42' AS DATETIME); Result: '2018-08-18 10:38:42'
Convert to TIME
The CAST() function can be used to convert a value to a TIME type. For example - in the example below, '10:38:42' is converted to TIME datatype.
SELECT CAST('10:38:42' AS TIME); Result: '10:38:42'
Convert to DECIMAL
The CAST() function can be used to convert a value to a DECIMAL type. For example - in the example below, '123.456' is converted to DECIMAL datatype.
SELECT CAST('123.456' AS DECIMAL(5, 2)); Result: 123.46
Convert to CHAR
The CAST() function can be used to convert a value to a CHAR type. For example - in the example below, 123 is converted to CHAR datatype.
SELECT CAST(123 AS CHAR); Result: '123'
Convert to NCHAR
The CAST() function can be used to convert a value to a NCHAR type. For example - in the example below, 123 is converted to NCHAR datatype.
SELECT CAST(123 AS NCHAR); Result: '123'
Convert to SIGNED
The CAST() function can be used to convert a value to a SIGNED type. For example - in the example below, 10-20 is converted to SIGNED datatype.
SELECT CAST(10-20 AS SIGNED); Result: -10
Convert to UNSIGNED
The CAST() function can be used to convert a value to a UNSIGNED type. For example - in the example below, 10-20 is converted to UNSIGNED datatype.
SELECT CAST(10-20 AS UNSIGNED); Result: 18446744073709551606
Convert to BINARY
The CAST() function can be used to convert a value to a BINARY type. For example - in the example below, 123 is converted to BINARY datatype.
SELECT CAST(123 AS BINARY); Result: '123'
❮ MariaDB Functions