MariaDB TO_BASE64() Function

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

The result encoded using the TO_BASE64() function can be decoded using the FROM_BASE64() function.

Unlike existing base-64 schemes, MariaDB TO_BASE64() and FROM_BASE64() functions have some special rules:

  • The encoding for the letter value 62 is '+'.
  • The encoding for the letter value 63 is '/'.
  • Encoded output consists of 4 printable characters. Every 3 bytes of input data are encoded using 4 characters. If the length of the last group is less than 4, it will be filled with '=' characters.
  • Adds a newline after every 76 characters of encoded output to break long output across multiple lines.
  • Decoding recognizes and ignores newlines, carriage returns, tabs, and spaces.

MariaDB TO_BASE64() Syntax

Here is the syntax of the MariaDB TO_BASE64() function:

TO_BASE64(str)

Parameters

str

Required. String for base-64encoding.

If you do not provide this parameter, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'TO_BASE64'.

Return value

The TO_BASE64(str) function encodes the given string in base-64 form and returns the encoded string representation.

The TO_BASE64() function returns NULL if the str argument is NULL.

MariaDB TO_BASE64() Examples

This statement uses MariaDB TO_BASE64() function to encode hello:

SELECT TO_BASE64('hello');

Output:

+--------------------+
| TO_BASE64('hello') |
+--------------------+
| aGVsbG8=           |
+--------------------+

Note that the TO_BASE64() function is case-sensitive, and the results of strings with different case are different. The following example illustrates this:

SELECT
  TO_BASE64('hello'),
  TO_BASE64('HELLO');

Output:

+--------------------+--------------------+
| TO_BASE64('hello') | TO_BASE64('HELLO') |
+--------------------+--------------------+
| aGVsbG8=           | SEVMTE8=           |
+--------------------+--------------------+

For encoded results of TO_BASE64(), you can use FROM_BASE64() to decode them:

SELECT
  FROM_BASE64('aGVsbG8='),
  FROM_BASE64('SEVMTE8=');

Output:

+-------------------------+-------------------------+
| FROM_BASE64('aGVsbG8=') | FROM_BASE64('SEVMTE8=') |
+-------------------------+-------------------------+
| hello                   | HELLO                   |
+-------------------------+-------------------------+

Conclusion

The MariaDB TO_BASE64() function returns the base-64 encoded string representation of the given string.