CONV

CONV is a SQL function used to convert numbers between different number bases. It can handle base conversions between base 2 (binary) and base 36.

CONV(N, from_base, to_base)

  • n: This parameter refers to the number you want to convert. In MySQL, this parameter should be a string.
  • from_base: This parameter dictates the base of the number in the ‘N’ parameter. It is the base from which you want to convert. The base value must be between 2 and 36, inclusive.
  • to_base: This parameter indicates the base to which you want the number to be converted. The base value must be between 2 and 36, inclusive.

Example

SELECT EXTRACT(YEAR FROM '2021-06-15'), CONV(EXTRACT(YEAR FROM '2021-06-15'), 10, 2);

Output

2021, 11111100101

Explanation

In the SQL code above, the EXTRACT(YEAR FROM date) function is used to extract the year from a given date. The CONV(value, from_base, to_base) function then converts the extracted year (which is in base 10) to base 2. Hence, the year ‘2021’ gets converted to ‘11111100101’.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.