×

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 Descending Index

Descending Index It is a type of index that stores key values in descending order. This query improves the performance of an index query. MySQL system displays the index as per...

3 minutes read.

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...

3 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 Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL Create index

An index creation helps to make a row of the table unique. The index operates and handles table data quickly. MySQL index requires NOT NULL column constraint. The index column...

2 minutes read.

MySQL FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

3 minutes read.

MySQL PERIOD_ADD() function

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

2 minutes read.

MySQL LOAD_FILE() function

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

3 minutes read.

MySQL Operators

MySQL Operators MySQL operator needs advanced operation on the table and its data. This operator works with the "WHERE" clause. MySQL operators are a statement to modify information. It helps to...

13 minutes read.

MySQL ASIN() Function

In this context we will learn how we can use the ASIN() function in MySQL with proper syntax and good example. Introduction of MySQL ASIN() function The arc sine value of a...

2 minutes read.

MySQL Command line client Basic Queries

MySQL – command line client Basic Queries There are many queries to work with the MySQL command-line client. Today, we will explore MySQL basic queries and their function. The MySQL query...

8 minutes read.

MySQL GREATEST function

This statement displays the greatest values of the table. The GREATEST function returns a null value when the table contains a null value. The GREATEST function needs a minimum of...

2 minutes read.

MySQL Features

Features of MySQL MySQL is a relational database management system (RDBMS): It is a collection of many programs and makes relations with many other programs. Easy to use MySQL database: MySQL...

2 minutes read.

MySQL LOG10() function

In this context, we will learn how we can use the MySQL LOG10() function with proper syntax and good examples. Introduction of MySQL LOG10() function To evaluate the natural logarithmic value of...

2 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 NATURAL JOIN

The natural join combines multiple tables using a column with the same name and data type. This operation combines a row of the two tables using common columns. This join...

4 minutes read.

MySQL MIN function

The MIN() function determines the minimum or lowest value of the data set. This function works on numerical data type values. If the table displays zero value, then the row...

4 minutes read.

MySQL Constraints

MySQL Constraints Introduction The constraints help to restrict what values should be stored in a table. The constraints provide limitations of the columns or data. This function helps to insert data in...

20 minutes read.

MySQL LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 minutes read.

MySQL MOD() function

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

3 minutes read.