BigQuery · Utility function
BigQuery SHA256
SHA256(input) returns the 32-byte SHA-256 hash of a STRING or BYTES value as BYTES. Use TO_HEX(SHA256(x)) for the standard 64-character hex digest. Use it for deterministic IDs, integrity checks and pseudonymized join keys; for plain bucketing, FARM_FINGERPRINT is cheaper.
SHA256 returns raw BYTES, which the console shows in base64. TO_HEX(SHA256(x)) produces the 64 lowercase hex characters other tools print, and LENGTH(SHA256(x)) is always 32. Snowflake's and MySQL's SHA2(x, 256) return hex text directly; BigQuery has no SHA2 function, but SHA1 and SHA512 exist for other digest sizes.
A common pattern is a deterministic record ID from a natural key: TO_HEX(SHA256(CONCAT(source, ':', external_id))) gives the same value on every run, unlike GENERATE_UUID, so re-runs and backfills stay idempotent. Put a separator between the parts, because CONCAT('ab', 'c') and CONCAT('a', 'bc') hash identically.
Hashing personal data such as emails is pseudonymization, not anonymization: anyone with a list of candidate emails can hash them and match. Normalize the input first (LOWER(TRIM(email))) so the same address always produces the same hash. SHA256 costs more compute than FARM_FINGERPRINT, so prefer the latter for sampling and bucketing.
Syntax
SHA256(input)Arguments and return value
| Argument | Description |
|---|---|
input | A STRING or BYTES value. A STRING is hashed as its UTF-8 bytes. |
Returns: BYTES (32 bytes). NULL for NULL input.
Examples
Hex digest and length
SELECT
TO_HEX(SHA256('order:10045')) AS sha256_hex,
LENGTH(SHA256('order:10045')) AS digest_bytes;| sha256_hex | digest_bytes |
|---|---|
| ea9aeba377b1603591e26f1267c115eef540bbb865027fb9157ea5ff4f841d3a | 32 |
Deterministic record keys
The same source and ID always produce the same key, and the source prefix keeps equal IDs from different systems apart.
WITH source_rows AS (
SELECT 'shopify' AS source, '10045' AS external_id UNION ALL
SELECT 'stripe', '10045'
)
SELECT
source,
TO_HEX(SHA256(CONCAT(source, ':', external_id))) AS record_key
FROM source_rows
ORDER BY source;| source | record_key |
|---|---|
| shopify | f4251b51d6574a6f2b1164c8e5a0b655f2d3ce8e349d2ee6717b0392a9114bfb |
| stripe | 55de5d4147ad92b334443c1f195662f7a42d0e6be9eadfaf29194966b9bb6b4a |
Hash a normalized email
LOWER and TRIM remove case and whitespace differences, so every spelling of the address maps to the hash of jane.doe@example.com.
SELECT
TO_HEX(SHA256(LOWER(TRIM(' Jane.Doe@Example.com ')))) AS email_sha256;| email_sha256 |
|---|
| 86e0b9e56c17cc4d12387e1949b85053fbe73bc3ce5a1188713a9d300cc6133d |
Separators matter
Without a separator, two different pairs of values form the same input string and get the same hash.
SELECT
SHA256(CONCAT('ab', 'c')) = SHA256(CONCAT('a', 'bc')) AS without_separator,
SHA256(CONCAT('ab', '|', 'c')) = SHA256(CONCAT('a', '|', 'bc')) AS with_separator;| without_separator | with_separator |
|---|---|
| true | false |
Common mistakes
- Comparing raw SHA256 output with hex strings. SHA256 returns BYTES; apply TO_HEX on the BigQuery side or FROM_HEX on the other value.
- Calling
SHA2(x, 256)from Snowflake or MySQL. It fails with Function not found; useTO_HEX(SHA256(x)). - Treating hashed emails or phone numbers as anonymous. They can be matched by hashing candidate values, so protect them like the raw data.
- Hashing without normalizing. Case, spaces and formatting differences produce completely different digests.
- Using SHA256 for sampling or sharding. FARM_FINGERPRINT is cheaper and returns an INT64 you can pass to MOD directly.
Cost and performance
- SHA256 is more expensive to compute than FARM_FINGERPRINT, which the GoogleSQL reference recommends when you only need high-throughput bucketing or sharding.
- A SHA256 digest takes 34 logical bytes as BYTES and 66 as a hex STRING, against 8 for an INT64 key, so store BYTES when you keep digests in large tables.
FAQ
How do I get a SHA256 hex string in BigQuery?
Use TO_HEX(SHA256(value)), which returns the 64-character lowercase hex digest. Use TO_BASE64(SHA256(value)) for base64.
Does BigQuery have a SHA2 function?
No. Use SHA256 or SHA512, and SHA1 for legacy needs. SHA2(x, 256) from Snowflake or MySQL corresponds to TO_HEX(SHA256(x)).
Will SHA256 in BigQuery match hashes from Python or other systems?
Yes, for the same input bytes. SHA-256 is a standard algorithm, so TO_HEX(SHA256('abc')) equals the hex digest any other SHA-256 implementation computes for the UTF-8 bytes of 'abc'.
Related
More hash and utility functions
All BigQuery hash and utility functions · All functions · BigQuery error messages
