How TO_BASE64() Works in MariaDB

In MariaDB, TO_BASE64() is a built-in string function that converts its string argument to its base-64 encoded form.

The TO_BASE64() function returns its result as a character string in the connection character set and collation (see how to get your connection’s collation).

Syntax

The syntax goes like this:

TO_BASE64(str)

Where str is the string to encode as base-64.

Example

Here’s a basic example:

SELECT TO_BASE64('Greenshank');

Result:

+-------------------------+
| TO_BASE64('Greenshank') |
+-------------------------+
| R3JlZW5zaGFuaw==        |
+-------------------------+

Strings can be decoded from base-64 with the FROM_BASE64() function. Therefore, we can use the result we get here, and use FROM_BASE64() to decode it:

SELECT FROM_BASE64('R3JlZW5zaGFuaw==');

Result:

+---------------------------------+
| FROM_BASE64('R3JlZW5zaGFuaw==') |
+---------------------------------+
| Greenshank                      |
+---------------------------------+

We can see that it results in the same string that we encoded with TO_BASE64() (although, FROM_BASE64() returns its result as a binary string).

Null Values

Providing null as an argument results in null:

SELECT TO_BASE64(null);

Result:

+-----------------+
| TO_BASE64(null) |
+-----------------+
| NULL            |
+-----------------+

Providing No Arguments

Calling TO_BASE64() without passing any arguments results in an error:

SELECT TO_BASE64();

Result:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'TO_BASE64'