SQL MD5 function
Returns the 128-bit MD5 hash of a string as a hex digest. Fine for checksums, unsafe for anything an attacker controls.
- Catégorie
- Strings
- Returns
string- In this category
- 123 of 172
Syntaxe
MD5(string)
SQL Server: HASHBYTES('MD5', value)
Oracle: STANDARD_HASH(value, 'MD5')
Each block is a separate form. The support table and the examples show which engine takes which.
Prise en charge des moteurs
| Moteur | Support | Notes |
|---|---|---|
| SQL Server | Different name or syntax | HASHBYTES('MD5', value) returns 16 raw bytes. |
| PostgreSQL | Oui | md5(text) returns lowercase hex. |
| MySQL | Oui | MD5(str) returns lowercase hex. |
| SQLite | Not built in | No MD5; hash in the application or via an extension. |
| Oracle | Different name or syntax | STANDARD_HASH(value, 'MD5') returns RAW bytes. |
Exemples
MySQL
SELECT MD5('hello') AS digest; -- 5d41402abc4b2a76b9719d911017c592
PostgreSQL
SELECT md5(payload) AS digest FROM messages;
SQL Server
SELECT LOWER(CONVERT(varchar(32), HASHBYTES('MD5', 'hello'), 2)) AS digest;
Bon à savoir
- MySQL and PostgreSQL return 32 lowercase hex characters. SQL Server's HASHBYTES and Oracle's STANDARD_HASH return 16 raw bytes; CONVERT(varchar(32), ..., 2) and RAWTOHEX render them as uppercase hex.
- MD5 has practical collisions, so do not use it where an attacker picks the input: signatures, tamper checks, passwords. Passwords need a slow salted function such as bcrypt or Argon2, computed in the application.
- It is still fine as a fast fingerprint for cache keys, change detection, and comparing rows across systems, provided both sides hash the same bytes in the same encoding.
Questions about MD5
Is MD5 safe for passwords?
No. MD5 is fast by design, which is exactly wrong for passwords. Use bcrypt, scrypt or Argon2 in application code, never a SQL hash function.
What is the difference between MD5 and SHA-256?
Digest size and collision resistance: MD5 produces 128 bits and has practical collisions; SHA-256 produces 256 bits and has none known.
Why does my MD5 result look different in SQL Server?
HASHBYTES returns binary. CONVERT(varchar(32), HASHBYTES('MD5', v), 2) gives uppercase hex without 0x; wrap it in LOWER() to match MySQL and PostgreSQL. Also hash varchar, not nvarchar, if the other side hashed UTF-8 or ASCII bytes.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.