MySQL Tutorial MySQL Advanced MySQL Database Account Management MySQL References

MySQL FROM_BASE64() Function



The MySQL FROM_BASE64() function decodes the given base-64 encode string and returns the result as a binary string. The result is NULL if the argument is NULL or not a valid base-64 string. This function is the reverse of the TO_BASE64() function.

Different base-64 encoding schemes exist. These are the encoding and decoding rules used by TO_BASE64() and FROM_BASE64():

  • The encoding for alphabet value 62 is '+'.
  • The encoding for alphabet value 63 is '/'.
  • Encoding output is made up of groups of four printable characters, with each three bytes of data encoded using four characters. If the final group is not complete, it is padded with '=' characters to make up a length of four.
  • To divide long output, a newline is added after every 76 characters.
  • Decoding recognizes and ignores newline, carriage return, tab, and space.

Syntax

FROM_BASE64(string)

Parameters

string Required. Specify the base-64 encoded string.

Return Value

Returns the decoded string as a binary string. Returns NULL if the argument is NULL or not a valid base-64 string.

Example 1:

The example below shows the usage of FROM_BASE64() function.

mysql> SELECT TO_BASE64('Hello');
Result: 'SGVsbG8='

mysql> SELECT FROM_BASE64(TO_BASE64('Hello'));
Result: 'Hello'

mysql> SELECT FROM_BASE64('SGVsbG8=');
Result: 'Hello'

Example 2:

Consider a database table called Sample with the following records:

DataEncodedName
Data 1Sm9obg==
Data 2TWFycnk=
Data 3Sm8=
Data 4S2lt
Data 5UmFtZXNo

The statement given below can be used to decode the records of column EncodedName. This column contains base-64 encoded records.

SELECT *, FROM_BASE64(EncodedName) AS FROM_BASE64_Value FROM Sample;

This will produce the result as shown below:

DataEncodedNameFROM_BASE64_Value
Data 1Sm9obg==John
Data 2TWFycnk=Marry
Data 3Sm8=Jo
Data 4S2ltKim
Data 5UmFtZXNoRamesh

❮ MySQL Functions