Convert binary string to bigint in MySQL?

Sean Owen picture Sean Owen · Aug 11, 2009 · Viewed 12.2k times · Source

I am attempting to hash a string to a 64-bit value (bigint) in MySQL. I am aware of the MD5() function, which returns a 128-bit hash as a binary string. I'd be happy to just take the bottom or top 64 bits of this result. However, I cannot figure out how to get from a binary string type to a numeric type of any sort. Any pointers?

Answer

Ken Keenan picture Ken Keenan · Aug 11, 2009

Use the CONV() function to convert the MD5 hash from base 16 to base 10 and CAST to convert it to a number:

select cast(conv(substring(md5(id), 1, 16), 16, 10) as unsigned integer) from SomeTable;