Convert a stored md5 string to a decimal value in MySQL

后端 未结 2 1632
时光说笑
时光说笑 2021-01-19 09:17

I have a very large table in MySQL. I\'m using a CHAR(32) field which contains an MD5 as a string of course. I\'m running into an issue where I need to convert this to a dec

相关标签:
2条回答
  • 2021-01-19 09:52

    Beware MD5 are 16 Byte long, and BIGINT UNSIGNED is 8 Byte long, so even in your second case you don't get the right answer, the number can't fit you are receiving the value of the lowest 8 Byte=> 09e91518db3e79d3.

    0 讨论(0)
  • 2021-01-19 09:59

    conv() is limited to 64 bit integers. You can convert the high and low part to decimal and then add them together:

    > select cast(conv(substr("000002dcc38af6f209e91518db3e79d3", 1, 16), 16, 10) as
                  decimal(65))*18446744073709551616 +
             cast(conv(substr("000002dcc38af6f209e91518db3e79d3", 17, 16), 16, 10) as
                  decimal(65));
    58055532535286745202684464101843
    

    Where 18446744073709551616 = 2^64. So in your case:

    > select cast(conv(substr(md5_key, 1, 16), 16, 10) as 
                  decimal(65))*18446744073709551616 +
             cast(conv(substr(md5_key, 17, 16), 16, 10) as
                  decimal(65))
             from bigtable limit 1;
    
    0 讨论(0)
提交回复
热议问题