RianAcademy
Search
โŒ˜
K
Premium

Type conversion functions, CAST

When we perform operations on values with different data types, the DBMS tries to perform a conversion and cast the values used to the desired type. For example, in the example below we are comparing values with STRING and INT types. To perform this comparison, the DBMS automatically will convert a string value to a numeric value.

MySQL 8.4
SELECT '50' > 49 AS comparison_1, '50' > 51 AS comparison_2;
comparison_1comparison_2
10

But not all DBMS conversions can be done automatically, and then it is necessary to do an explicit type conversion.

To do this, MySQL has two very similar functions CAST and CONVERT.

Syntax

MySQL 8.4
CAST(value AS type)
CONVERT(value, type)
value

What to convert: a number, a string, a date, or a table column.

type

The type the value is converted to. The available types are listed below.

Example,

MySQL 8.4
SELECT CAST(12005.6 AS DECIMAL) AS cast_example, CONVERT(12005.4, DECIMAL) AS convert_example;
cast_exampleconvert_example
1200612005

The CAST function can convert the passed value to any of the following types:

TypeDescription
DATEConverts a value to DATE. Format: "YYYY-MM-DD".
DATETIMEConverts a value to DATETIME. Format: "YYYY-MM-DD hh:mm:ss".
TIMEConverts a value to TIME. Format: "hh:mm:ss".
DECIMAL[(M[,D])]Converts a value to DECIMAL. It has two optional arguments M and D, which define the maximum total number of digits and the number of digits after the decimal point, respectively. By default, D is 0 and M is 10.
CHAR[(N)]Converts value to CHAR. You can pass the maximum length of the string as an optional argument.
SIGNEDConverts a value to a BIGINT value.
UNSIGNEDConverts a value to an unsigned BIGINT value.
BINARYConverts a value to BINARY.
YEARConverts a value to a year.

Impossibility of any conversion

Using the CAST function imposes requirements on the format of the original value. And the question immediately arises, what happens if the given format does not match the required one? For example, if you try to convert random text to a temporal data type:

MySQL 8.4
SELECT CAST('RianAcademy' AS DATETIME) AS invalid_cast;
invalid_cast
<NULL>

In this case, MySQL will return NULL instead of the converted value.

Self test

So, what is the responsibility of the CAST function in SQL ๐Ÿง?

Tasks for self-testing
0 / 1
1. Departure time

Print the flight identifier id and the departure time without the date departure_time for the flights from the Trip table that depart at 12:00 or later.
Company
id
INT
name
VARCHAR
Passenger
id
INT
name
VARCHAR
Pass_in_trip
id
INT
trip
INT
passenger
INT
place
VARCHAR
Trip
id
INT
company
INT
plane
VARCHAR
town_from
VARCHAR
town_to
VARCHAR
time_out
DATETIME
time_in
DATETIME
ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”ื”
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Result
Company
Passenger
Pass_in_trip
Trip

Run a query to see the result, or open a table above