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

  • 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.


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 |


Comments loading...