UNBASE64

Decode a Base64 string (RFC 4648) to binary data.

Category: hashReturns: BINARYDialect: Standard

Syntax

UNBASE64(str)

Description

## Overview Decodes a Base64-encoded text string into binary data using the standard alphabet defined in RFC 4648. Use this function to recover bytes from text-serialized payloads (for example API bodies, JSON fields, log lines) for comparison, verification, or rehashing. UNBASE64 is the inverse of BASE64. Invalid input raises an error rather than silently truncating. ## Behavior - Returns NULL for NULL input. - Accepts the standard alphabet: A-Z, a-z, 0-9, '+', '/', with '=' padding. - Raises an error on characters outside the alphabet or on invalid padding. - Whitespace inside the input is not accepted by default; strip before calling. - Empty input returns empty binary. - Output is BYTEA. ## Algorithm - Base64 per RFC 4648 (standard, not URL-safe). - Output length is 3 * floor(n/4) bytes minus 0, 1, or 2 bytes depending on padding. ## Compatibility - Alphabet and padding match RFC 4648 Section 4. - URL-safe Base64 (Section 5) must be remapped ('-' to '+', '_' to '/') and re-padded before decoding.

Parameters

NameTypeDescription
strSpecifies the Base64-encoded text to decode. Must contain only characters from the standard Base64 alphabet (A-Z, a-z, 0-9, '+', '/', '='). Returns NULL for NULL input.

Examples

-- Decode a Base64 string and cast back to text
SELECT CAST(UNBASE64('aGVsbG8=') AS STRING) AS text;  -- 'hello'
-- Empty input yields empty output
SELECT UNBASE64('') AS bytes;
-- Round-trip with BASE64
SELECT CAST(UNBASE64(BASE64('test data')) AS STRING) AS back;
-- Decode a binary payload
SELECT UNBASE64('3q2+7w==') AS bytes;  -- 0xDEADBEEF
-- Realistic: decode signatures stored as text
SELECT event_id, UNBASE64(signature_b64) AS signature_bytes
FROM security.audit.signed_events
WHERE created_date = DATE '2026-04-19';

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →