BASE64ENCODE function

BASE64ENCODE is a function that manipulates all string data types (BIT, BLOB, and CHARACTER).

BASE64ENCODE returns a CHARACTER string that contains the base64-encoded version of the source string.

Syntax

Read syntax diagramSkip visual syntax diagramBASE64ENCODE( SourceExpression )

BASE64ENCODE returns a CHARACTER string containing a base64 representation of the source string. The source string can be a string of the CHARACTER, BLOB, or BIT data type. If SourceExpression is NULL, the result is NULL.

If SourceExpression is of CHARACTER type, it is first converted to the UTF-8 code page before encoding as base64.
If SourceExpression is of BLOB type, it is encoded as base64 directly, without any prior changes.
If SourceExpression is of BIT type, it is first CAST to BLOB before encoding as base64 and so its length must be a multiple of 8.

Examples

The base64 encoding of a BLOB source string and subsequent decoding back to BLOB is shown by the following example:
DECLARE original BLOB X'48656c6c6f';
DECLARE encoded CHARACTER BASE64ENCODE(original);
DECLARE decoded BLOB BASE64DECODE(encoded);
The base64 encoding of a CHARACTER source string that is first automatically converted to UTF-8 and later decoded is shown by the following example:
DECLARE original CHARACTER 'Hello World!';
DECLARE encoded CHARACTER BASE64ENCODE(original);
DECLARE decoded BLOB BASE64DECODE(encoded);
DECLARE decoded2 CHARACTER CAST(decoded AS CHARACTER CCSID 1208);
The base64 encoding of a BIT source string that is first automatically converted to a BLOB and later decoded is shown by the following example:
DECLARE original BIT B'0010001001000001';
DECLARE encoded CHARACTER BASE64ENCODE(original);
DECLARE decoded BLOB BASE64DECODE(encoded);
DECLARE decoded2 BIT CAST(decoded AS BIT);
Encoding from a CHARACTER source string to a BLOB and back to CHARACTER again in a code page other than UTF-8 is shown by the following example:
DECLARE original CHARACTER 'Hello World!';
DECLARE originalBlob BLOB CAST(original AS BLOB CCSID 819);
DECLARE encoded CHARACTER BASE64ENCODE(originalBlob);
DECLARE decoded BLOB BASE64DECODE(encoded);
DECLARE decoded2 CHARACTER CAST(decoded AS CHARACTER CCSID 819);