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.
- 카테고리
- Strings
- Returns
string / binary- In this category
- 69 of 172
구문
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.
엔진 지원
| 엔진 | Support | 메모 |
|---|---|---|
| SQL Server | 예 | Supported under this name. |
| PostgreSQL | 예 | Supported under this name. |
| MySQL | 예 | Supported under this name. |
| SQLite | Not built in | no built-in hash function; extensions provide md5/sha |
| Oracle | 예 | Supported under this name. |
예시
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;
알아 두면 좋은 정보
- 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.
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.