// strings reference

SQL string hashing

Compute a cryptographic digest of a value in-query for dedupe, change detection and joins. The function names and output types vary; the caveats do not.

Catégorie
Strings
Returns
string / binary
In this category
69 of 172

Syntaxe

MD5(s)
HASHBYTES('SHA2_256', s)
STANDARD_HASH(s)

Each block is a separate form. The support table and the examples show which engine takes which.

Prise en charge des moteurs

MoteurSupportNotes
SQL Server Oui Supported under this name.
PostgreSQL Oui Supported under this name.
MySQL Oui Supported under this name.
SQLite Not built in no built-in hash function; extensions provide md5/sha
Oracle Oui Supported under this name.

Exemples

PostgreSQL

SELECT md5('abc') AS h;  -- 900150983cd24fb0d6963f7d28e17f72

SQL Server

SELECT CONVERT(VARCHAR(64), HASHBYTES('SHA2_256', 'abc'), 2) AS h;

MySQL

SELECT SHA2('abc', 256) AS h;

Oracle

SELECT STANDARD_HASH('abc', 'SHA256') AS h FROM dual;

Bon à savoir

  • Output types differ: MySQL's MD5 returns lowercase hex text, SQL Server's HASHBYTES returns binary (CONVERT(..., 2) renders hex without 0x), Oracle's STANDARD_HASH returns RAW.
  • PostgreSQL: md5() is built in and returns hex text; sha256() (PG 11+) returns bytea: encode(sha256(convert_to(s, 'utf8')), 'hex') for the hex form.
  • Encoding matters: the hash covers the stored bytes, so latin1 and utf8mb4 columns hash the same visible text differently.
  • Use hashes for dedupe and comparison only, never for passwords, which need salted, slow KDFs done outside the query.

Questions about Hashing strings

Which engines can hash in SQL?

MySQL (MD5, SHA1, SHA2), PostgreSQL (md5, sha2xx family), SQL Server (HASHBYTES) and Oracle (STANDARD_HASH). SQLite has none built in.

Why do my hashes differ between systems?

Different input encodings (bytes, not characters, are hashed), different algorithms, or hex case. Pin the algorithm, the encoding, and the rendering first.

Is MD5 acceptable for deduping rows?

Yes. Collisions are practically irrelevant there. For integrity or security-sensitive checks, use SHA-256.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.

// fewer lookups

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.