Type Conversion
Implicit type conversion takes place when MariaDB is using operands or different types, in order to make the operands compatible.
It is best practice not to rely upon implicit conversion; rather use CAST to explicitly convert types.
Rules for conversion on comparison
- If either argument is NULL, the result of the comparison is NULL unless the NULL-safe <=> equality comparison operator is used.
- If both arguments are integers, they are compared as integers.
- If both arguments are strings, they are compared as strings.
- If one argument is decimal and the other argument is decimal or integer, they are compared as decimals.
- If one argument is decimal and the other argument is a floating point, they are compared as floating point values.
- If a hexadecimal argument is not compared to a number, it is treated as a binary string.
- If a constant is compared to a TIMESTAMP or DATETIME, the constant is converted to a timestamp, unless used as an argument to the IN function.
- In other cases, arguments are compared as floating point, or real, numbers.
Examples
Converting a string to a number:
MariaDB [(none)]> SELECT 15+'15'; +---------+ | 15+'15' | +---------+ | 30 | +---------+
Converting a number to a string:
MariaDB [(none)]> SELECT CONCAT(15,'15'); +-----------------+ | CONCAT(15,'15') | +-----------------+ | 1515 | +-----------------+
Floating point number errors:
MariaDB [(none)]> SELECT '9746718491924563214' = 9746718491924563213; +---------------------------------------------+ | '9746718491924563214' = 9746718491924563213 | +---------------------------------------------+ | 1 | +---------------------------------------------+
Rules for conversion on dyadic arithmetic operations
Implicit type conversion also takes place on dyadic arithmetic operations (+,-,*,/). MariaDB chooses the minimum data type that guarantees to fit the result.
For addition (+), subtraction (-), multiplication (*), the result is chosen as follows:
- If either of the arguments is an approximate number (float, double), the result is double.
- If either of the arguments is a string (char, varchar, text), the result is double.
- If either of the arguments is a decimal number, the result is decimal.
- If either of the arguments is of a temporal type with a non-zero fractional second precision (time(N), datetime(N), timestamp(N)), the result is decimal.
- If either of the arguments is of a temporal type with a zero fractional second precision (time(0), date, datetime(0), timestamp(0)), the result may vary between int, int unsigned, bigint, bigint unsigned, depending on the exact data type combination.
- If both arguments are integer numbers (tinyint, smallint, mediumint, bigint), the result may vary between int, int unsigned, bigint, bigint unsigned, depending of the exact data types and their signess.
For division (/), the result is chosen as follows:
- If either of the arguments is an approximate number (float, double), the result is double.
- If either of the arguments is a string (char, varchar, text), the result is double.
- Otherwise, the result is decimal.
Note, the above rules mean that when an argument of a temporal data type appears in addition or subtraction, it's treated as a number by default.
MariaDB [test]> SELECT TIME'10:20:30' + 1; +--------------------+ | TIME'10:20:30' + 1 | +--------------------+ | 102031 | +--------------------+
In order to do a temporal arithmetic instead, use functions DATE_ADD(), DATE_SUB(), or the INTERVAL specifier for the second argument:
MariaDB [test]> SELECT TIME'10:20:30' + INTERVAL 1 SECOND; +------------------------------------+ | TIME'10:20:30' + INTERVAL 1 SECOND | +------------------------------------+ | 10:20:31 | +------------------------------------+