×

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: Entity-Relationship Model

Entity-Relationship Model Entity-Relationship model or E R model is used to create a relationship between different attributes or entities. It describes the structure of the database with the help of the...

5 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 Triggers

MySQL trigger is a function of the stored procedure to respond to the system program. This function responds and runs any data table event automatically. You can use it for...

5 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 FIND_IN_SET() 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 MySQL provides a built-in string function called...

4 minutes read.

MySQL Character Length Function

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

2 minutes read.

MySQL String Function

The string function is used to maintain and operate string values. This function modifies the string data as per function. We can do the concatenation, conversion, removal and replacing the...

4 minutes read.

MySQL LOG() function

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

3 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 RIGHT() Function

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

3 minutes read.

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 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 CEIL() function

In this context, we will learn how we can use the MySQL CEIL () function with proper syntax and good examples. Introduction of MySQL CEIL() function Input is taken by the CEIL()...

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 Math Function

The math function operates numerical values and displays required number data. The math function finds out sin, cos, tan values. Here, you find out the absolute value, reminder, and logarithmic...

6 minutes read.

MySQL Database Introduction

MySQL - Database Introduction: The database plays an essential role in MySQL for storing and modifying the data. The database creates and keeps multiple tables. The database organizes and manipulates...

8 minutes read.

MySQL EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL Union

MySQL contains the same categories of data in a different table. Sometimes, you require multiple tables to collect data together. MySQL union is a function to combine two table's data...

5 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 MAX function

The MAX function is a type of aggregation function that determines the maximum value of the table data. This function works on numerical data type values. If the table displays...

4 minutes read.