HEX() :
This function in MySQL is used to return an equivalent hexadecimal string value of a string or numeric Input. If the input is a string then each byte of each character in the string is converted to two hexadecimal digits. This function also returns a hexadecimal string representation of the numeric argument N treated as a longlong (BIGINT) number.
Syntax :
HEX(string)
OR
HEX(N)
Parameter :
This method accepts only one parameter.
- string - Input string who's each character is to be converted to two hexadecimal digits.
- N - Input number which is to be converted to hexadecimal.
Returns :
It returns an equivalent hexadecimal string representation of a string or numeric Input.
Example-1 :
Hexadecimal representation of the decimal number 0 using HEX Function as follows.
SELECT HEX(0) AS Hex_number ;
Output :
Example-2 :
Hexadecimal representation of the decimal number 2020 using HEX Function as follows.
SELECT HEX( 2020 ) AS Hex_number ;
Output :
Example -3 :
Hexadecimal representation of the string 'geeksforgeeks' using HEX Function as follows.
SELECT HEX( 'geeksforgeeks') AS Hex_string ;
Output :
HEX_STRING |
---|
6765656B73666F726765656B73 |
Example-4 :
Using HEX Function to find a hexadecimal representation of all decimal numbers present in a column as follows.
Creating a Player table :
CREATE TABLE Player(
Player_id INT AUTO_INCREMENT,
Player_name VARCHAR(100) NOT NULL,
Playing_team VARCHAR(20) NOT NULL,
Highest_Run_Scored INT NOT NULL,
PRIMARY KEY(Player_id )
);
Inserting data into the Table :
INSERT INTO
Player(Player_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 ) ;
To verify use the following command as follows.
SELECT * FROM Player;
Output :
PLAYER_ID | PLAYER_NAME | PLAYING_TEAM | HIGHEST_RUN_SCORED |
---|
1 | Virat Kohli | RCB | 60 |
2 | Rohit Sharma | MI | 45 |
3 | Dinesh Karthik | KKR | 26 |
4 | Shreyash Iyer | DC | 40 |
5 | David Warner | SRH | 65 |
6 | Steve Smith | RR | 52 |
7 | Andre Russell | KKR | 70 |
8 | Jasprit Bumrah | MI | 10 |
9 | Risabh Panth | DC | 34 |
Now, we will find the highest run scored by each player in hexadecimal using the HEX Function.
SELECT
Player_id, Player_name,
Playing_team, HEX(HIGHEST_RUN_SCORED) AS HighestRunInHexaDecimal
FROM Player ;
Output :
PLAYER_ID | PLAYER_NAME | PLAYING_TEAM | HighestRunInHexaDecimal |
---|
1 | Virat Kohli | RCB | 3C |
2 | Rohit Sharma | MI | 2D |
3 | Dinesh Karthik | KKR | 1A |
4 | Shreyash Iyer | DC | 28 |
5 | David Warner | SRH | 41 |
6 | Steve Smith | RR | 34 |
7 | Andre Russell | KKR | 46 |
8 | Jasprit Bumrah | MI | A |
9 | Risabh Panth | DC | 22 |
Similar Reads
HOUR() Function in MySQL
The HOUR() function in MySQL is a powerful tool for extracting the hour component from a given time or DateTime expression. Whether working with time, datetime or string values that can be interpreted as time, the HOUR() function returns the hour as an integer ranging from 0 to 23. In this article,
3 min read
MySQL IF( ) Function
The MySQL IF() function is a control flow function that returns different values based on the result of a condition. IF() Function in MySQLThe IF() function in MySQL returns a value if the condition is TRUE and another value if the condition is FALSE. The MySQL IF() function can return values that c
2 min read
LN() Function in MySQL
LN() function : It is the function in MySQL is used to calculate the natural logarithm of a specific number with base e . The number must be greater than 0, otherwise it will return NULL. Syntax : LN(X) Parameter : LN() function accepts one parameter as mentioned above in the syntax and described be
2 min read
PI() function in MySQL
PI() function in MySQL is used to return the Pi value. The default number of decimal places displayed is seven, but MySQL uses the full double-precision value internally. Syntax : PI() Parameter : This method does not accept any parameter. Returns : It returns the Pi value i.e. 3.141593. Example-1 :
2 min read
DIV() Function in MySQL
DIV() function : This function in MySQL is used to return a quotient (integer) value when integer division is done. For example, when 7 is divided by 3, then 2 will be returned. Syntax : SELECT x DIV y; Parameter : This method accepts two parameters as given below as follows. x - Specified dividend
1 min read
GREATEST() function in MySQL
The GREATEST() function in MySQL is designed to return the largest value from a list of expressions. It is particularly useful for identifying the maximum value among multiple columns or literal values, simplifying tasks that involve comparisons of multiple values. In this article, We will learn abo
4 min read
DAY() Function in MySQL
DAY() function : This function in MySQL is used to return the day of the month for a specified date (a number from 1 to 31). This function equals the DAYOFMONTH() function. Syntax : DAY(date) Parameter : This method accepts a parameter which is illustrated below : date : Specified date to extract th
1 min read
SUM() Function in MySQL
The SUM() function in MySQL is a powerful aggregate function used to calculate the total sum of values in a numeric column. By summing up the values in the specified column, this function helps in generating overall totals and performing calculations that provide meaningful insights from our data. I
4 min read
TAN() Function in MySQL
TAN() function : This function in MySQL is used to return the tangent of a specified number. In any right triangle, the tangent of an angle is the length of the opposite side divided by the length of the adjacent side. Similarly, this can also be defined as tangent of x is the sine of x divided by t
1 min read
COT() Function in MySQL
COT() function : This function in MySQL is used to return the cotangent of a specified number. If the specified number is 0, an error or NULL will be returned. In a right triangle, the cotangent of an angle is the length of it's adjacent side divided by the length of the opposite side. Similarly, th
1 min read