How REVERSE() Works in MariaDB

In MariaDB, REVERSE() is a built-in string function that returns a string with the characters in reverse order.

The function accepts one argument: the string to reverse.

Syntax

The syntax goes like this:

REVERSE(str)

Where str is the string to reverse.

Example

Here’s a basic example:

SELECT REVERSE('abc');

Result:

+----------------+
| REVERSE('abc') |
+----------------+
| cba            |
+----------------+

Database Example

Here’s an example that reverses the data returned from a database column:

SELECT 
    PetName,
    REVERSE(PetName) 
FROM Pets;

Result:

+---------+------------------+
| PetName | REVERSE(PetName) |
+---------+------------------+
| Fluffy  | yffulF           |
| Fetch   | hcteF            |
| Scratch | hctarcS          |
| Wag     | gaW              |
| Tweet   | teewT            |
| Fluffy  | yffulF           |
| Bark    | kraB             |
| Meow    | woeM             |
+---------+------------------+

Empty String

Passing an empty string returns an empty string:

SELECT REVERSE('');

Result:

+-------------+
| REVERSE('') |
+-------------+
|             |
+-------------+

Null Arguments

Providing null results in null:

SELECT REVERSE(null);

Result:

+---------------+
| REVERSE(null) |
+---------------+
| NULL          |
+---------------+

Missing Argument

Calling REVERSE() with the wrong number of arguments, or without passing any arguments results in an error:

SELECT REVERSE();

Result:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1