MySQL UNHEX()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 13, 2024
Contribute to Docs

The UNHEX() function is a build-in function in MySQL that converts a hexadecimal string to its corresponding binary string. Useful when we have data stored in hexadecimal format we can use UNHEX() function to convert it to the original binary form making it usable and understandable.

  • Learn to analyze data with SQL and prepare for technical interviews.
    • Includes 9 Courses
    • With Certificate
    • Beginner Friendly.
      18 hours
  • In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
    • Beginner Friendly.
      5 hours

Syntax

UNHEX(val);
  • val: A hexadecimal string to be converted to its corresponding binary form.

Example

The below example shows how UNHEX() function works.

CREATE TABLE documents (
id INT PRIMARY KEY,
content BLOB
);
INSERT INTO documents (id, content) VALUES (1, UNHEX('636F6465636164656D79'));
SELECT HEX(content) AS 'hex-content', CAST(content AS CHAR) AS readable_content
FROM documents
WHERE id = 1;

The output of the above code will be:

hex-content readable-content
636F6465636164656D79 codecademy

All contributors

Contribute to Docs

Learn MySQL on Codecademy

  • Learn to analyze data with SQL and prepare for technical interviews.
    • Includes 9 Courses
    • With Certificate
    • Beginner Friendly.
      18 hours
  • In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
    • Beginner Friendly.
      5 hours