CONV()
The CONV()
function in MySQL is used to convert a number from one base to another and returns the result as a string representing the converted value.
It allows you to perform base conversions between numeric systems like binary, octal, decimal, and hexadecimal.
Syntax
CONV(number, from_base, to_base)
number
: The number to be converted.from_base
: The base of the number to be converted.to_base
: The base to which the number should be converted.
Note: The
from_base
andto_base
parameters define the numeric bases for the conversion and must be between 2 and 36. The function operates on positive integers and returns the result as a string.
Example
Assume we have a numbers
table with a binary_value
column containing numbers in binary format:
numbers
Table
binary_value |
---|
1010 |
1101 |
1111 |
The following example demonstrates how to convert the binary values in the binary_value
column to their decimal equivalents:
SELECT binary_value, CONV(binary_value, 2, 10) AS decimal_valueFROM numbers;
In this example, the CONV()
function converts the binary_value
from base 2 (binary) to base 10 (decimal). The output will be as follows:
binary_value | decimal_value |
---|---|
1010 | 10 |
1101 | 13 |
1111 | 15 |
If you want to convert a hexadecimal value to decimal, assume the hex_value
column in the numbers
table:
hex_value |
---|
A |
D |
F |
You can convert the hexadecimal values to decimal with the following query:
SELECT hex_value, CONV(hex_value, 16, 10) AS decimal_valueFROM numbers;
The output will be as follows:
hex_value | decimal_value |
---|---|
A | 10 |
D | 13 |
F | 15 |
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn MySQL on Codecademy
- Skill path
Analyze Data with SQL
Learn to analyze data with SQL and prepare for technical interviews.Includes 9 CoursesWith CertificateBeginner Friendly17 hours - Skill path
Data Science Foundations
Learn to clean, analyze, and visualize data with Python and SQL.Includes 15 CoursesWith CertificateBeginner Friendly54 hours - Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly5 hours