RienSQL
LESSON 46 / 62 · Type conversion functions

Type conversion functions

SQL type conversion: CAST function in MySQL and PostgreSQL, CONVERT in MySQL. Data types for conversion, syntax and examples.

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.

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

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

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

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

Example,

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:

SELECT CAST('RianAcademy' AS DATETIME) AS invalid_cast;
invalid_cast

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 🧐?

The function is used when it is necessary to determine the data type of the passed value.The function is responsible for converting a value from one data type to anotherThe function is required to perform mathematical calculationsCheck

Practice

Solve it in the editor. The query runs against a sample database in your browser.

Task 1Departure 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.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.