With MySQL, we can do complex calculations very easily with the help of inbuilt mathematical functions. CONV(X) works in converting a number from one number system to another number system. Hence, making conversion of the number from Decimal to binary number system and vice versa and etc, more easy job.
CONV(number) works in converting a user-entered number from one number base system to another number base system. It takes 3 arguments i.e. input number X, base_from is the original base and base_to is the base in which we want to convert the number into.
It returns a string representation of a number X, converted from base base_from to base base_to.
It returns NULL, if any of the input arguments value is NULL.
The argument number is interpreted as an integer, but it can be specified as an integer or a string.
The minimum base is 2 i.e. Binary and the maximum base is 36 i.e. hexadecimal.
If base_from is a negative number, X is regarded as a signed number. Otherwise, X is treated as unsigned.
CONV() works with 64-bit precision.
Name, Required /Optional, Description
X, Required, It represents a valid number.
base_from, Required, Existing base of the number X.
base_to, Required, Base of the number X after conversion.
Return, Description
NULL, if the argument is NULL.
string representation of a number X, converted from base_from to base_to.
Below is an example of converting a number from base 10 (decimal) to base 2 (binary):
mysql> SELECT CONV(4, 10, 2); +----------------+ | CONV(4, 10, 2) | +----------------+ | 100 | +----------------+ 1 row in set (0.00 sec)
You can see that 4 in decimal converts to 100 in binary.
For converting decimal into binary in MySQL, there is one more function i.e. BIN(), which can do the same thing.
So, the above example is similar to the following:
mysql> SELECT BIN(4); +--------+ | BIN(4) | +--------+ | 100 | +--------+ 1 row in set (0.00 sec)
CONV() also provide us the feature of converting the binary value back to a decimal value. Let’s take the above example convert the binary value 100 to decimal value 4.
Below is the way to do this.
mysql> SELECT CONV(100, 2, 10); +------------------+ | CONV(100, 2, 10) | +------------------+ | 4 | +------------------+ 1 row in set (0.00 sec)
Below is an example of converting a number from base 10 (decimal) to base 16 (Hexadecimal). Below is the way to do this.
mysql> SELECT CONV(12789, 10, 16); +---------------------+ | CONV(12789, 10, 16) | +---------------------+ | 31F5 | +---------------------+ 1 row in set (0.00 sec)
For converting decimal into binary in MySQL, there is one more function i.e. HEX(), which can do the same thing. So, the above example is similar to the following:
mysql> SELECT HEX(12789); +------------+ | HEX(12789) | +------------+ | 31F5 | +------------+ 1 row in set (0.10 sec)
CONV() also provide us the feature of converting the Hexadecimal value back to a decimal value. Let’s take the above example convert the Hexadecimal value ’31F5′ to decimal value 12789.
Below is the way to do this.
mysql> SELECT CONV('31F5', 16, 10); +----------------------+ | CONV('31F5', 16, 10) | +----------------------+ | 12789 | +----------------------+ 1 row in set (0.01 sec)
Below is an example of converting a number from base 10 (decimal) to base 8 (Octal):
mysql> SELECT CONV(14, 10, 8); +-----------------+ | CONV(14, 10, 8) | +-----------------+ | 16 | +-----------------+ 1 row in set (0.00 sec)
You can see that 14 in decimal converts to 16 in Octal.
For converting decimal into binary in MySQL, there is one more function i.e. OCT(), which can do the same thing.
So, the above example is similar to the following:
mysql> SELECT OCT(14); +---------+ | OCT(14) | +---------+ | 16 | +---------+ 1 row in set (0.03 sec)
CONV() also provide us the feature of converting the binary value back to an Octal value. Let’s take the above example convert the octal value 16 to decimal value 14.
Below is the way to do this.
mysql> SELECT CONV(16, 8, 10); +-----------------+ | CONV(16, 8, 10) | +-----------------+ | 14 | +-----------------+ 1 row in set (0.00 sec)
See the below example :
mysql> SELECT CONV('test', 2, 10); +---------------------+ | CONV('test', 2, 10) | +---------------------+ | 0 | +---------------------+ 1 row in set, 1 warning (0.01 sec) mysql> SHOW WARNINGS; +---------+------+-------------------------------------------+ | Level | Code | Message | +---------+------+-------------------------------------------+ | Warning | 1292 | Truncated incorrect DECIMAL value: 'test' | +---------+------+-------------------------------------------+ 1 row in set (0.00 sec)
If any of the arguments is NULL, it will return NULL. See the below example :
mysql> SELECT CONV(NULL, 2, 10); +-------------------+ | CONV(NULL, 2, 10) | +-------------------+ | NULL | +-------------------+ 1 row in set (0.06 sec) mysql> SELECT CONV(4, NULL, 2); +------------------+ | CONV(4, NULL, 2) | +------------------+ | NULL | +------------------+ 1 row in set (0.00 sec) mysql> SELECT CONV(4, 2, NULL); +------------------+ | CONV(4, 2, NULL) | +------------------+ | NULL | +------------------+ 1 row in set (0.01 sec)
Below is an example of converting a number from base 10 (decimal) to base 36. Below is the way to do this.
mysql> SELECT CONV(12789, 10, 36); +---------------------+ | CONV(12789, 10, 36) | +---------------------+ | 9V9 | +---------------------+ 1 row in set (0.00 sec)
CONV() also provide us the feature of converting the Hexadecimal value back to a decimal value. Let’s take the above example convert the Hexadecimal value ‘9V9’ to decimal value 12789.
mysql> SELECT CONV('9V9', 36, 10); +---------------------+ | CONV('9V9', 36, 10) | +---------------------+ | 12789 | +---------------------+ 1 row in set (0.00 sec)
App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…
As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…
Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…
COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…
Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…
For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…