HEX() Function SQL

The HEX() function will operate on both strings and integers. It will convert integers to hexadecimal (base 16) numbers. For strings it will convert each character into a two-digit hexadecimal number and string them together without delimiters.

For a string:

HEX('go to the right')

will yield: 676F20746F20746865207269676874

For a number:

SELECT HEX(32), HEX('32')

We get two different results. If it is NOT in quotes it is evaluated as a number and the result would be 20. If it IS in quotes it will be evaluated as a string and give us 3332. In some applications the string conversion would be HEX_STRING('string'). If HEX is used with a variable, the results will be the same as in our last example.

SET @z = 32; SELECT HEX(@Z) , HEX('@Z');

The variable without quotes will be treated as a number and the variable with quotes will be treated as a string. If a non-numeric variable or string is tried without quotes, it will throw an error.

< FOUND ROWS Function | IFNULL Function SQL >

Interesting Pages