- Related Questions & Answers
- In MySQL, how FIELD() function is different from FIND_IN_SET() function?
- What happens if a NULL argument is provided in MySQL CONV() function?
- What is MySQL NULL-safe equal operator and how it is different from comparison operator?
- Do we have any lower and upper limit of base in MySQL CONV() function? What happens if out of limit base is provided in CONV() function?
- What is Shallow Copy and how it is different from Deep Copy in C#?
- What is MySQL ELT() function?
- What is MySQL MAKE_SET() function?
- What is MySQL OCTET_LENGTH() function?
- What is MySQL INTERVAL() function?
- What is the purpose of using MySQL CHAR_LENGTH() function? Which function is the synonym of it?
- What is a smartwatch? How is it different from other wearable devices?
- How MySQL LOCATE() function is different from its synonym functions i.e. POSITION() and INSTR() functions?
- What is Natural Therapy? How is it different from other forms of medicines?
- What is MySQL STRCMP() function and what would be the output of this function?
- What MySQL ASCII() function returns if no parameter is provided to it?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Actually, HEX() function converts the decimal or string value into a hexadecimal value. After conversion MySQL returns a string representation of that hexadecimal value.

HEX(Num or Str)

As we know that HEX() function can convert a number or string hence ‘Num’ in the syntax represents the number which is to be converted to hexadecimal and ‘Str’ is a string whose character is to be converted to two hexadecimal digits.

mysql> Select HEX(210); +----------+ | HEX(210) | +----------+ | D2 | +----------+ 1 row in set (0.00 sec)

In the above example, 210 is a decimal number which is converted to a hexadecimal string representation and it is treated as a BIGINT number.

mysql> SELECT HEX('NULL'); +-------------+ | HEX('NULL') | +-------------+ | 4E554C4C | +-------------+ 1 row in set (0.00 sec)

In the above example, ‘NULL’ is a string whose characters are converted to two hexadecimal digits (per character two hexadecimal digits).

Basically, MySQL HEX() function is equivalent to CONV(N,10,16) but the basic difference is that HEX() can convert string character to two hexadecimal digits but CONV() returns 0 on trying to convert string characters into a hexadecimal string. It is demonstrated in the following example −

mysql> Select HEX('N'); +----------+ | HEX('N') | +----------+ | 4E | +----------+ 1 row in set (0.00 sec) mysql> Select CONV('N',10,16); +-----------------+ | CONV('N',10,16) | +-----------------+ | 0 | +-----------------+ 1 row in set (0.00 sec)

Advertisements