Oggu Oggu - 2 years ago 150
SQL Question

UnHex a value Or Column in MySQL

select hex(28) from dual; -- 1C
select unhex(1C) from dual; -- Error 1054 : Unknown Column '1C'

What should i use in MySQL to Unhex to achieve the result in a decimal value 28 ?

My End Goal is to perform a OR operation, so am trying :

select b'(binary(unhex(1C))' | b'(binary(unhex(F))' from dual; -- Error 1064

How do i achive the above statement ?

Answer Source

Since you can't be bothered to click the link I gave you, I'll copy the relevant (opening) paragraphs here. It never ceases to amaze me how lazy some people are.

As documented under Hexadecimal Literals:

MySQL supports hexadecimal values, written using X'val', x'val', or 0xval format, where val contains hexadecimal digits (0..9, A..F). Lettercase of the digits does not matter. For values written using X'val' or x'val' format, val must contain an even number of digits. For values written using 0xval syntax, values that contain an odd number of digits are treated as having an extra leading 0. For example, 0x0a and 0xaaa are interpreted as 0x0a and 0x0aaa.

In numeric contexts, hexadecimal values act like integers (64-bit precision). In string contexts, they act like binary strings, where each pair of hex digits is converted to a character:


SELECT 0x1c | 0xf

See it on sqlfiddle.

Next time, when someone tries to be helpful and suggests that you read the relevant bit of documentation, perhaps give that a go instead of expecting to be spoon-fed like a baby?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download