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
Output
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’.