BigQuery · Utility function
BigQuery MD5
MD5(input) returns the 16-byte MD5 hash of a STRING or BYTES value as BYTES, which the console shows in base64. Wrap it in TO_HEX to get the familiar 32-character hex string. Use it for checksums and change detection, never for security.
MD5 returns BYTES, not text. The BigQuery console displays BYTES as base64, so MD5('ana@example.com') shows up as zbnWod3cN1oJzIPjABWY3A==. PostgreSQL, MySQL and Snowflake return the hex form directly; in BigQuery use TO_HEX(MD5(x)) to get the 32 lowercase hex characters other systems print.
Typical uses are checksums and change detection: MD5(TO_JSON_STRING(t)) fingerprints a whole row, so comparing it between two snapshots finds rows that changed in any column. Hash inputs are byte-exact: trailing spaces, letter case and different Unicode forms all change the result, so normalize with LOWER and TRIM first when matching values from different sources.
MD5 is broken as a cryptographic hash, and the GoogleSQL reference advises using it only where another system requires an MD5 digest. For new work, use SHA256 when you need a standard digest and FARM_FINGERPRINT when you need a fast INT64 for bucketing. A NULL input returns NULL.
Syntax
MD5(input)Arguments and return value
| Argument | Description |
|---|---|
input | A STRING or BYTES value. A STRING is hashed as its UTF-8 bytes. |
Returns: BYTES (16 bytes). NULL for NULL input.
Examples
BYTES vs hex output
The first column is the raw digest, shown in base64. TO_HEX gives the lowercase hex string most tools expect.
SELECT
MD5('ana@example.com') AS md5_bytes,
TO_HEX(MD5('ana@example.com')) AS md5_hex;| md5_bytes | md5_hex |
|---|---|
| zbnWod3cN1oJzIPjABWY3A== | cdb9d6a1dddc375a09cc83e3001598dc |
Find rows that changed between snapshots
TO_JSON_STRING turns each row into one string, so a single hash comparison covers every column. Customer 2 moved from Paris to Lyon.
WITH yesterday AS (
SELECT 1 AS customer_id, 'Ana' AS name, 'Berlin' AS city UNION ALL
SELECT 2, 'Ben', 'Paris'
),
today AS (
SELECT 1 AS customer_id, 'Ana' AS name, 'Berlin' AS city UNION ALL
SELECT 2, 'Ben', 'Lyon'
)
SELECT t.customer_id
FROM today AS t
JOIN yesterday AS y
ON t.customer_id = y.customer_id
WHERE MD5(TO_JSON_STRING(t)) != MD5(TO_JSON_STRING(y));| customer_id |
|---|
| 2 |
Normalize before hashing
Letter case and a trailing space change the hash. Lowercasing and trimming first makes both spellings match.
SELECT
MD5('Ana@Example.com ') = MD5('ana@example.com') AS raw_match,
MD5(LOWER(TRIM('Ana@Example.com '))) = MD5('ana@example.com') AS normalized_match;| raw_match | normalized_match |
|---|---|
| false | true |
Common mistakes
- Comparing MD5 output with hex strings from other systems. MD5 returns BYTES; compare
TO_HEX(MD5(x))with the other system's value. - Hashing values that differ only in case or whitespace. 'Ana@Example.com ' and 'ana@example.com' hash differently; apply LOWER and TRIM before MD5.
- Using MD5 for passwords, signatures or anything security-related. It isn't secure against collision attacks.
- Comparing TO_JSON_STRING hashes across a schema change. Adding a column changes every row's JSON and therefore every hash; hash an explicit column list when the schema evolves.
- Concatenating columns without a separator before hashing.
CONCAT('ab', 'c')andCONCAT('a', 'bc')are the same input; use a delimiter or TO_JSON_STRING.
Cost and performance
- Store digests as BYTES (18 logical bytes for MD5) rather than hex STRING (34 logical bytes) in large tables, and convert with TO_HEX only for display.
FAQ
How do I get an MD5 hex string in BigQuery?
Use TO_HEX(MD5(value)). It returns 32 lowercase hex characters, the same value that md5() returns in PostgreSQL or MySQL.
Why does MD5 return a value like zbnWod3cN1oJzIPjABWY3A==?
MD5 returns BYTES, and the console displays BYTES in base64. The digest is the same; TO_HEX converts it to hex and TO_BASE64 to a base64 string.
Should I use MD5, SHA256 or FARM_FINGERPRINT?
Use FARM_FINGERPRINT for fast INT64 bucketing and sampling, SHA256 for standard or security-relevant digests, and MD5 only when another system expects an MD5 value.
Related
More hash and utility functions
All BigQuery hash and utility functions · All functions · BigQuery error messages
