MySQL Tutorial MySQL Advanced MySQL Database Account Management MySQL References

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:

DataHEX
Data 1AA
Data 21A
Data 35F
Data 4F3
Data 5B8

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:

DataHEXDEC
Data 1AA170
Data 21A26
Data 35F95
Data 4F3243
Data 5B8184

❮ MySQL Functions