MySQL Tutorial

MySQL Tutorial MySQL Features MySQL Database Introduction MySQL Environmental Setup MySQL Data Types MySQL variable MySQL Advance table Query MySQL database queries MySQL Entity-Relationship Model MySQL Table Query MySQL Operators MySQL logical conditions MySQL Queries MySQL Clauses Clustered vs Non-Clustered Index MySQL Full text index MySQL Descending Index MySQL Invisible Index MySQL Composite Index MySQL Prefix index MySQL Index MySQL Create index MySQL Drop Index MySQL Show index MySQL Unique index MySQL Table MySQL Variable MySQL View MySQL Constraints MySQL Command Line Client Basic Queries MySQL Stored Procedure MySQL IF Statement MySQL Subquery MySQL Triggers

MySQL Join

MySQL Join MySQL CROSS JOIN MySQL DELETE JOIN MySQL EQUI JOIN MySQL INNER JOIN MySQL Union MySQL NATURAL JOIN MySQL RIGHT JOIN MySQL SELF JOIN MySQL UPDATE JOIN

MySQL Function

MySQL Function MySQL AVG() Function MySQL SUM() Function MySQL String() Function MySQL Advance() Function MySQL Aggregate() Function MySQL COALESCE() Function MySQL Control Flow Function MySQL COUNT() Function MySQL Date And Time Function MySQL GREATEST() Function MySQL ISNULL() Function MySQL LEAST() Function MySQL Math() Function MySQL MAX() Function MySQL MIN() Function MySQL find_in_set() function MySQL ASIN() Function MySQL CEIL() function MySQL CEILING() function MySQL TAN() Function MySQL Truncate() Function MySQL FLOOR() function MySQL LN() function MySQL LOG2() function MySQL LOG10() function MySQL MOD() function MySQL PI() function MySQL POW() function MySQL RADIANS() function MySQL RAND() function MySQL ROUND() function MySQL Character Length Function MySQL Current Date Function MySQL Date Add Function MySQL Date Format Function MySQL Datediff Function MySQL Day Function MySQL Elt Function MySQL Export Set Function MySQL Field Function MySQL Format Function MySQL From Base64 Function MySQL Hex Function MySQL Insert Function MySQL Instr Function MySQL Length Function MySQL CONCAT() function MySQL FIND_IN_SET() function MySQL LIKE() function MySQL LOAD_FILE() function MySQL LOCATE() function MySQL LOG() function MySQL MONTHNAME() function MySQL NOW() function MySQL PERIOD_ADD() function MySQL PERIOD_DIFF() function MySQL POWER() function MySQL QUARTER() function MySQL REVERSE() function MySQL RIGHT() Function MySQL RPAD() function MySQL RTRIM() function MySQL SEC_TO_TIME() function MySQL SOUNDEX() function

Questions

Which Clause is Similar to Having Clause in MySQL

Misc

MySQL Error 1046 - No Database Selected Failed to Start MySQL Service Unit MySQL Service Unit not Found Import MySQL Connector Mudule not Found Error No Module Named MySQL Joins Available in MySQL MySQL Docs MySQL Download For Windows 7 64 Bit MySQL Error Code 1064 MySQL Export MySQL History MySQL Host MySQL Import MySQL Drop All Tables MySQL Drop MySQL Error Code 1175 MySQL Events MySQL Except MYSQL Foreign Key Constraint MySQL If Exists MySQL IndexOf MySQL List All Tables json_extract in MySQL TIMESTAMPDIFF in MySQL MySQL Syntax Checker Sudo MySQL Secure Installation

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.