In MariaDB, CONV()
is a built-in numeric function that converts numbers between different number bases. For example, you can use it to convert a number from base 10 to base 16.
It returns a string representation of the converted number.
Syntax
The syntax goes like this:
CONV(N,from_base,to_base)
Where N
is the number to convert, from_base
is the base to convert from, and to_base
is the base to convert to.
The minimum base is 2 and the maximum base is 36.
Example
Here’s an example:
SELECT CONV(12, 10, 16);
Result:
+------------------+ | CONV(12, 10, 16) | +------------------+ | C | +------------------+
The result is C
because that’s the hexadecimal (base 16) value for 12
.
So the count goes like this: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C.
Here’s another one:
SELECT CONV(1234567, 10, 16);
Result:
+-----------------------+ | CONV(1234567, 10, 16) | +-----------------------+ | 12D687 | +-----------------------+
Negative Base
If the to_base
argument is a negative number, N
is regarded as a signed number. Otherwise it’s treated as unsigned.
SELECT CONV(-12, 10, -16);
Result:
+--------------------+ | CONV(-12, 10, -16) | +--------------------+ | -C | +--------------------+
Out of Range Base
The minimum base is 2 and the maximum base is 36. If the second or third argument isn’t within this range, null
is returned.
Example
SELECT CONV(12, 10, 42);
Result:
+------------------+ | CONV(12, 10, 42) | +------------------+ | NULL | +------------------+
String Arguments
The number is interpreted as an integer, but may be specified as an integer or a string.
SELECT CONV('C', 16, 2);
Result:
+------------------+ | CONV('C', 16, 2) | +------------------+ | 1100 | +------------------+
Null Arguments
CONV()
returns null
if any of the arguments are null
:
SELECT CONV(null, 10, 8);
Result:
+-------------------+ | CONV(null, 10, 8) | +-------------------+ | NULL | +-------------------+
Missing Arguments
Calling CONV()
with the wrong number of arguments, or without any arguments results in an error:
SELECT CONV();
Result:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'CONV'
And:
SELECT CONV(10, 2);
Result:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'CONV'