⌘BABAKOV.NET Let's code!
100% [light] [ru]

Type Casting in MySQL

This article will cover the main ways to perform type casting in MySQL. It should be noted that in most cases, type casting in MySQL will work automatically, for example:

//Выполнив запрос:
select 1+\'1\';
//Получим результат: 2


However, in some cases, special type casting is required, which will be discussed further.

Case 1 - String Comparison (BINARY keyword)

In the MySQL database, string comparison does not depend on the case of the compared strings. For example, if we compare the strings 'Chupacabra cool' and 'CHUPACABRA COOL', MySQL will consider them identical.

//Для
select "Чупакабра крут" = "ЧУПАКАБРА КРУТ";
//Получим результат: 1 //Тоесть True;


However, there is a method to bypass this feature, which involves comparing two strings as binary sequences, by converting the string to a binary sequence using the BINARY keyword.

//Например
select binary "Чупакабра крут" = "ЧУПАКАБРА КРУТ";
//Получим результат: 0 //Тоесть False;


The BYNARY keyword can be used in this case, either to the left or to the right of the comparison operator.

//Пример использования оператора слева
select binary "Чупракабра крут" = "ЧУПАКАБРА КРУТ";
//Пример использованиея оператора справа
select "Чупакабра крут" binary = "ЧУПАКАБРА КРУТ";


Case 2 - CAST function

MySQL has a CAST() function, which is a standard SQL function supported by many other databases as well.
The purpose of the CAST() function is to convert an expression from one type to another.

The function has the following syntax:

CAST(expr AS type)


The result of the function will be the converted expression expr to type type.

The type type can take the following values:

BINARY
CHAR
DATE
DATETIME
SIGNED [INTEGER]
TIME
UNSIGNED [INTEGER]


Example of function usage:

//Для запроса:
select 20091201 as field1, cast(20091201 as date) as field2;
//Получим результат:
field1       |       field2
20091201     |       2009-12-01


The CAST() function is often used to sort ENUM fields by alphabetical order instead of numerical value.
This is done by:

select * from table order by CAST(field as char);
//Где field - поле типа ENUM


The function is also often used to convert date format:

select now();
//Получим результат: 2011-03-30 10:12:11
select cast(now() as date);
//Получим результат: 2011-03-30



Case 3 - CONVERT function

The third option for changing data type is using the CONVERT() function, which has the following syntax:

//Вариант №1
convert(expr, type);
//Вариант №2
convert(expr USING charset);


The first variant of the CONVERT() function is identical to the CAST() function.

The second variant of the CONVERT() function is used to convert text from one encoding to another.

Example:

select convert(\'Чупакабра крут\' using koi8r);
//Вернёт результат:
"вСОЮЙЮАПЮ ЙПСР"


There is also an alternative syntax:

select _koi8r\'Чупакабра крут\';
//Вернёт результат:
"вСОЮЙЮАПЮ ЙПСР"

At this point, the converted string is preceded by the name of the encoding, followed by an underscore.

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.