Convert a stored MD5 string to a decimal value in MySQL?


You can use the conv() function along with the cast() to cast from hexadecimal to a decimal value.

Note − MD5 is in hexadecimal

Let us first create a table −

mysql> create table DemoTable
(
   Password text
);
Query OK, 0 rows affected (0.60 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values("a5391e96f8d48a62e8c85381df108e98");
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values("ea7a32d2dc5bb793af262dcb6ea1a54d");
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------------------------------+
| Password                         |
+----------------------------------+
| a5391e96f8d48a62e8c85381df108e98 |
| ea7a32d2dc5bb793af262dcb6ea1a54d |
+----------------------------------+
2 rows in set (0.00 sec)

Following is the query to convert a stored md5 string to a decimal value in MySQL −

mysql> select cast(conv(substr(Password, 1, 16), 16, 10) as decimal(65))*18446744073709551616
+ cast(conv(substr(Password, 17, 16), 16, 10) as decimal(65)) AS DecimalValue
from DemoTable;

This will produce the following output −

+-----------------------------------------+
| DecimalValue                            |
+-----------------------------------------+
| 219619200658969319114298942978912194200 |
| 311673842057003455136843080376797734221 |
+-----------------------------------------+
2 rows in set (0.00 sec)

Updated on: 03-Oct-2019

336 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements