×

MySQL HEX() function

In this context, we will learn how we can use the MySQL HEX() function with proper syntax and good examples.

Introduction of MySQL HEX() function

For returning an equivalent hexadecimal string value of a string or numeric Input, the HEX() function is used in MySQL. Each byte of each character in the string will be converted into two hexadecimal digits when the Input is a string. A hexadecimal string representation of the numeric argument N is also returned by this function which is treated as a longlong (BIGINT) number.

Syntax of the MySQL HEX() function

The syntax of the MySQL HEX() function is given as follows:

HEX(str)
OR
HEX(N)

Parameters or arguments used in MySQL HEX() function:

There is only one parameter accepted by the HEX() function in MySQL, which is given as follows:

Str – Input string whose each character is to be converted to two hexadecimal digits.

N – Input number, which is to be converted to hexadecimal.

Returns:

It will return an equivalent hexadecimal string representation of a string or numeric Input.

Application used for HEX() function:

The HEX() function can be used in the given below MySQL versions.:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

Examples of MySQL HEX() function:

Now we will look into some MySQL HEX() function examples and will explore how we can use the HEX function in MySQL.

Example-1:

In this example, we will derive the Hexadecimal representation of the decimal number 0 using the HEX Function in MySQL as follows.

SELECT HEX(0) AS Hex_number ;

Output :

HEX_NUMBER
0
1 row in set (0.00 sec)

Example-2 :

Here, we will derive the Hexadecimal representation of the decimal number 2020 using the HEX Function in MySQL as follows.

SELECT HEX( 2020 ) AS Hex_number ;

Output :

HEX_NUMBER
7E4
1 row in set (0.00 sec)

Example -3 :

Here, we will derive the Hexadecimal representation of the string 'javaTpoint' using the HEX Function in MySQL as follows.

SELECT HEX( 'javaTpoint') AS Hex_string ;

Output :

HEX_STRING
6A61766154706F696E74
1 row in set (0.00 sec)

Example-4 :

Here, we will use the HEX Function to derive a hexadecimal representation of all decimal numbers in a column.

Creating a Cricketer table:-

CREATE TABLE Cricketer(


Cricketer_id INT AUTO_INCREMENT,
Cricketer_name VARCHAR(100) NOT NULL,
Playing_team VARCHAR(20) NOT NULL,
Highest_Run_Scored INT NOT NULL,
PRIMARY KEY(Cricketer_id )
);

Now we will insert the data into the Table.

INSERT INTO
Cricketer(Cricketer_name, Playing_team, Highest_Run_Scored)
VALUES
('Virat Kohli,' 'RCB,' 60 ),
('Rohit Sharma,' 'MI,' 45),
('Dinesh Karthik', 'KKR', 26 ),
('Shreyash Iyer,' 'DC,' 40 ),
('David Warner', 'SRH', 65),
('Steve Smith,' 'RR,' 52 ),
('Andre Russell', 'KKR', 70),
('Jasprit Bumrah', 'MI', 10),
('Risabh Panth,' 'DC,' 34 ) ;

For verification, we will use the following command as follows.

SELECT * FROM Cricketer;

Output:

CRICKETER_IDCRICKETER_NAMEPLAYING_TEAMHIGHEST_RUN_SCORED
1Virat KohliRCB60
2Rohit SharmaMI45
3Dinesh KarthikKKR26
4Shreyash IyerDC40
5David WarnerSRH65
6Steve SmithRR52
7Andre RussellKKR70
8Jasprit BumrahMI10
9Risabh PanthDC34

Now, we will find the highest run scored by each Cricketer in hexadecimal using the HEX Function.

SELECT
Cricketer_id, Cricketer_name,
Playing_team, HEX(HIGHEST_RUN_SCORED) AS HighestRunInHexaDecimal
FROM Cricketer ;

Output:

CRICKETER_IDCRICKETER_NAMEPLAYING_TEAMHighestRunInHexaDecimal
1Virat KohliRCB3C
2Rohit SharmaMI2D
3Dinesh KarthikKKR1A
4Shreyash IyerDC28
5David WarnerSRH41
6Steve SmithRR34
7Andre RussellKKR46
8Jasprit BumrahMIA
9Risabh PanthDC22

Application of MySQL HEX() function:

This function is used to return an equivalent hexadecimal string value of a string or numeric Input.

Summary:

In the above context, we have learned how we can use the HEX() function in MySQL used to return an equivalent hexadecimal string value of a string or numeric Input.


Related Topics

MySQL vs SQL

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL function

MySQL function is a piece of program to perform some specific task. Here we pass a parameter and then return a single value. MySQL function mainly supports string, numeric, conditional,...

9 minutes read.

MySQL SOUNDEX() function

In this context, we will learn how we can use the MySQL SOUNDEX() function with proper syntax and good examples. Introduction of MySQL SOUNDEX() function SOUNDEX() function in MySQL is used to...

3 minutes read.

MySQL Data Types

Introduction of the Data types In the data file, we have several information such as names, numbers, marks, and other information. This information specifies categories such as numbers, characters, and decimal...

8 minutes read.

MySQL CEILING() function

In this context, we will learn how we can use the MySQL CEILING () function with proper syntax and good examples. Introduction of MySQL CEILING () function The CEILING() function accepts the...

2 minutes read.

MySQL Clauses

MySQL Clauses MySQL system clauses are keywords or statements to handle information. It helps to operate a group of the data and apply it to require conditions. The clauses apply conditions...

16 minutes read.

MySQL FORMAT() function

In this context, we will learn how we can use the MySQL FORMAT() function with proper syntax and good examples. Introduction of MySQL FORMAT() function This function in MySQL helps to format...

2 minutes read.

MySQL Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

3 minutes read.

MySQL Advance function

The advance function operates numerical values, string values, and data types.  The advance function converts, displays, and compares given values as per requirement. Here, you find out database, table, and...

4 minutes read.

MySQL DATE_FORMAT() function

In this context, we will learn how we can use the MySQL DATE_FORMAT() function with proper syntax and good examples. Introduction of MySQL DATE_FORMAT() function DATE_FORMAT() function in MySQL is used to...

3 minutes read.

MySQL IF statement

The IF statement shows the true condition of the control flow function. The first condition is necessary to fulfill the requirement. The other condition is optional in the "IF" statement....

3 minutes read.

MySQL Date and Time function

The date function displays day, year, month, time, and current date. It shows the date and time as per the requirement of the applications. The data either store on the...

8 minutes read.

MySQL FROM_BASE64() function

In this context, we will learn how we can use the MySQL FROM_BASE64()() function with proper syntax and good examples. Introduction of MySQL FROM_BASE64()() function Basically, the FROM_BASE64() function In MySQL decodes...

2 minutes read.

MySQL Control flow function

The control flow function uses values or operands for logical operations. These functions can works on single and multiple conditions. The control flow function is based on the Boolean expression....

3 minutes read.

MySQL SEC_TO_TIME() function

In this context, we will learn how we can use the MySQL SEC_TO_TIME() function with proper syntax and good examples. Introduction of MySQL SEC_TO_TIME() function This function in MySQL is used to...

2 minutes read.

MySQL logical conditions

MySQL logical conditions Introduction MySQL handles data with clauses, operators, and conditions. The logical condition is used to compare information and returns the required output. This condition applies logic to MySQL expressions...

7 minutes read.

MySQL ELT() function

In this context, we will learn how we can use the MySQL ELT() function with proper syntax and good examples. Introduction of MySQL ELT() function In the ELT function, the number field...

2 minutes read.

MySQL CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 minutes read.

MySQL SUM function

The SUM() function displays the total addition of the table values. It supports the arithmetic operation of the column values. This function does not return a null value. Here, the...

5 minutes read.

MySQL database queries

MySQL database queries Database queries help to create, use, show and, delete the database. A Database is a collection of several tables and data. The database uses SQL language to perform...

3 minutes read.