MySQL FROM_BASE64() function
In this context, we will learn how we can use the MySQL FROM_BASE64()() function with proper syntax and good examples.
Introduction of MySQL FROM_BASE64()() function
Basically, the FROM_BASE64() function In MySQL decodes a base-64 encoded string which gives us the result. More specifically, it takes a string encoded with the base-64 encoding rules used by TO_BASE64(), and the decoded result as a binary string is returned by it.
Syntax of the MySQL FROM_BASE64()() function
The syntax of the MySQL FROM_BASE64()() function is given as follows
FROM_BASE64(str);
Parameters or arguments used in MySQL FROM_BASE64()() function:
There is only one parameter accepted by the FROM_BASE64()() function in MySQL, which are given as follows:
Str- it is the base-64 encoded string that we want to decode.
Returns:
It will return the decoded result as a binary string.
Application used for FROM_BASE64()() function:
The FROM_BASE64()() function can be used in the given below MySQL versions.:
- MySQL 5.7
- MySQL 5.6
- MySQL 5.5
- MySQL 5.1
- MySQL 5.0
- MySQL 4.1
- MySQL 4.0
- MySQL 3.23
Examples of MySQL FROM_BASE64()() function:
Now we will look into some MySQL FROM_BASE64()() function examples and will explore how we can use the FROM_BASE64() function in MySQL.
Example 1 – Basic Usage of MYSQL from_base() FUNCTION
Here's an example for a demonstration of the basic usage:
SELECT FROM_BASE64('Q2F0');
Output:
Cat
1 row in set (0.00 sec)
In this SELECT query, our argument is Q2F0, which is the base-64 encoded string for Cat.
Now, we are going to find the base-64 encoded string by passing Cat to the TO_BASE64() function:
SELECT TO_BASE64('Cat');
Output:
Q2F0
1 row in set (0.00 sec)
Example 2 – Example of a Longer String
Here's an example which uses a longer string:
SELECT FROM_BASE64('TXkgY2F0IGxpa2VzIHRvIGNoYXNlIGVsZXBoYW50cyE=');
Output:
My Cat likes to chase elephants!
1 row in set (0.00 sec)
Example 3 – Invalid Argument
If the argument is not a valid base-64 string, NULL will be returned:
SELECT FROM_BASE64('Oops!');
Output:
NULL
1 row in set (0.00 sec)
Example 4 – NULL Argument
We will also get NULL if we pass NULL string as an argument:
SELECT FROM_BASE64(NULL);
Output:
FROM_BASE64(NULL)
NULL
1 row in set (0.00 sec)
Example 5 – Missing Argument
We will get an error if we don't pass argument in the function:
SELECT FROM_BASE64();
Output:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'FROM_BASE64.'
Example 6 – Too Many Arguments
We'll also get an error if we pass too many arguments:
SELECT FROM_BASE64('Q2F0', 'RWxlcGhhbnQ=');
Output:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'FROM_BASE64.'
Application of MySQL FROM_BASE64()() function:
This function is used to decode a base-64 encoded string in MySQL.
Summary:
In the above context, we have learned how we can use the FROM_BASE64()() function in MySQL used to take a string encoded with the base-64 encoding rules used by TO_BASE64() and will return the decoded result as a binary string.