MySQL CONV() Function
The CONV()
function in MySQL converts a number from one base (numerical system) to another. This is particularly useful when working with binary, hexadecimal, or other non-decimal number systems.
CONV(): Definition and Usage
CONV()
takes a number and two base values as input. It converts the number from the source base to the target base. The result is always returned as a string. If any of the input parameters are NULL
, the result will be NULL
.
Syntax
Syntax
CONV(number, from_base, to_base)
Parameter Values
Parameter | Description |
---|---|
number |
The number to convert. This is required. |
from_base |
The base (numerical system) of the input number (2-36). This is required. |
to_base |
The target base (2-36 or -2 to -36; negative values for lowercase hexadecimal). This is required. |
Examples
Converting from Base 10 to Base 2
This example converts the decimal number 15 to its binary equivalent.
Syntax
SELECT CONV(15, 10, 2);
Output
1111
Converting from Base 2 to Base 10
This example converts the binary number 1111 to decimal.
Syntax
SELECT CONV(1111, 2, 10);
Output
15
Converting from Base 10 to Base 16
Converting the decimal number 88 to hexadecimal.
Syntax
SELECT CONV(88, 10, 16);
Output
58