MySQL CONV() Function
The MySQL CONV() function converts a number from one numeric base system to another, and returns the result as a string value. Note the following while using this function:
- This function returns NULL if any of the parameters are NULL.
- If a positive to_base is specified, this function treats number as an unsigned number.
- If a negative to_base is specified, this function treats number as a signed number.
- When this function is used with CONV(number, 10, 2) syntax, it is equivalent to using the BIN() function.
Syntax
CONV(number, from_base, to_base)
Parameters
number |
Required. Specify the number to convert. |
from_base |
Required. Specify the numeric base system of number. It can be between 2 and 36. |
to_base |
Required. Specify the numeric base system to convert to. It can be between 2 and 36, or -2 and -36. |
Return Value
Returns the result of conversion of a number from one numeric base system to another as a string.
Example 1:
The example below shows the usage of CONV() function.
mysql> SELECT CONV(20, 10, 2); Result: '10100' mysql> SELECT CONV(20, 10, 8); Result: '24' mysql> SELECT CONV(30, 10, 16); Result: '1E' mysql> SELECT CONV(-30, 10, -16); Result: '-1E' mysql> SELECT CONV(111, 2, 10); Result: '7' mysql> SELECT CONV(71, 8, 10); Result: '57' mysql> SELECT CONV('FF', 16, 10); Result: '255'
Example 2:
Consider a database table called Sample with the following records:
Data | HEX |
---|---|
Data 1 | AA |
Data 2 | 1A |
Data 3 | 5F |
Data 4 | F3 |
Data 5 | B8 |
The statement given below can be used to get the decimal representation of hexadecimal values of column HEX.
SELECT *, CONV(HEX, 16, 10) AS DEC FROM Sample;
This will produce the result as shown below:
Data | HEX | DEC |
---|---|---|
Data 1 | AA | 170 |
Data 2 | 1A | 26 |
Data 3 | 5F | 95 |
Data 4 | F3 | 243 |
Data 5 | B8 | 184 |
❮ MySQL Functions