How to Do Type Casting in MySQL

By default, MySQL is not strict with type casting. For example, adding a numeric value in string quotes to another numeric value with not give the usual errors other databases and programming languages will give:

mysql> select 1 + '1';
+---------+
| 1 + '1' |
+---------+
|       2 |
+---------+

However, should the need arise, you can use the CAST() function to force the type of a value. You can cast to the following types: BINARY, CHAR, DATE, DATETIME, TIME,DECIMAL, SIGNED, UNSIGNED.

-- cast float to unsigned integer
SELECT CAST(1.0 AS UNSIGNED);

-- cast string to date
SELECT CAST('2018-12-12' AS DATE);

-- cast string to decimal
SELECT CAST('12.345' AS DECIMAL(5,3));

-- cast string to time:
SELECT CAST('12:45' AS TIME);