BASE64

Encode binary data or a string as a Base64 string (RFC 4648).

Category: hashReturns: STRINGDialect: Standard

Syntax

BASE64(expr)

Description

## Overview Encodes binary data or a string as a Base64 text string using the standard Base64 alphabet defined in RFC 4648. String inputs are first converted to their UTF-8 byte representation before encoding. Use BASE64 to represent arbitrary bytes as safe text for transport (HTTP headers, JSON fields, email) or for human-readable logging. Pair with UNBASE64 for round-trip conversion. ## Behavior - Returns NULL for NULL input. - Output uses the standard Base64 alphabet: A-Z, a-z, 0-9, '+', '/'. - Output is padded with '=' to a multiple of 4 characters. - No line wrapping is applied. - String inputs are encoded as UTF-8 bytes first. - Empty input returns an empty string. - Output is a STRING. ## Algorithm - Base64 per RFC 4648 (standard, not URL-safe). - Output length is 4 * ceil(n/3) characters for n input bytes. ## Compatibility - Alphabet and padding match RFC 4648 Section 4 (standard Base64). - For URL-safe Base64 (RFC 4648 Section 5) the '+' and '/' characters must be post-processed to '-' and '_'.

Parameters

NameTypeDescription
exprSpecifies the value to encode. String values are first converted to their UTF-8 byte representation. Returns NULL for NULL input.

Examples

-- Encode a simple string
SELECT BASE64('hello') AS b64;  -- 'aGVsbG8='
-- Empty input yields empty output
SELECT BASE64('') AS b64;
-- Encode binary produced by UNHEX
SELECT BASE64(UNHEX('DEADBEEF')) AS b64;  -- '3q2+7w=='
-- Round-trip through UNBASE64
SELECT CAST(UNBASE64(BASE64('test')) AS STRING) AS back;
-- Realistic: expose signed payload to logs
SELECT event_id, BASE64(signature_bytes) AS signature_b64
FROM security.audit.signed_events
WHERE created_date = DATE '2026-04-19';

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →