Bug #17258 Cast of x'hexstring' fails to cast if odd length hexstring is given.
Submitted: 9 Feb 2006 1:43 Modified: 9 Feb 2006 12:41
Reporter: Ralf Leidinger Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Charsets Severity:S4 (Feature request)
Version:5.0.18-nt-max OS:Windows (windows xp sp2)
Assigned to: Assigned Account CPU Architecture:Any

[9 Feb 2006 1:43] Ralf Leidinger
Description:
Server: 5.0.18-nt-max running as service on Windows XP SP2
Client: mysql  Ver 14.12 Distrib 5.0.18, for Win32 (ia32)

Using the x'hexstring' syntax as described in http://dev.mysql.com/doc/refman/5.0/en/hexadecimal-values.html, fails for numerical hexstrings when casted implicit or explicit using CAST() if the length of the given hexstring is odd; 

How to repeat:
These will fail:

mysql> select x'1' + 1;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'x'1'
+ 1' at line 1

select cast(x'1' as unsigned); => same error

select x'123' + 0; => same error
select cast(x'123' as unsigned); => same error

These will work:

select x'01' + 0;
    => 1

select x'0123' + 0;
    => 291

Suggested fix:
Workaround by using only hexstrings with even length.
[9 Feb 2006 10:53] Valeriy Kravchuk
Thank you for a problem report. Sorry, but it is not a bug. MySQL works with 1 or more bytes of data, for all data types. 1 byte is represented with 2 hexademical digits. Hence the failure to cast half-bytes to anything in any context.
[9 Feb 2006 12:41] Valeriy Kravchuk
I changed my mind after some internal discussion. It is a reasonable feature request, easy to implement. SELECT 0x1 works anyway, so my reasoning above was not really valid.