w3resource

MySQL REVERSE() function

REVERSE() function

MySQL REVERSE() function reverses a string supplied as an argument.

This function is useful in -

  • String reversal: It allows us to reverse the order of characters in a string.
  • Palindrome detection: REVERSE() is often used to check whether a string is a palindrome, which means it remains the same when its characters are reversed.
  • Data transformation: REVERSE() can be used to transform data by changing the order of characters.

Syntax:

REVERSE(str)

Where str is a string.

Note: This function is multi-byte safe.

Syntax Diagram:

MySQL REVERSE() Function - Syntax Diagram

MySQL Version: 8.0

MySQL: REVERSE() function - w3resource

The following MySQL statement returns the string given in the argument in reverse order.

Code:

SELECT REVERSE('w3resource');

Output:

mysql> SELECT REVERSE('w3resource');
+-----------------------+
| REVERSE('w3resource') |
+-----------------------+
| ecruoser3w            | 
+-----------------------+
1 row in set (0.02 sec)

Example MySQL REVERSE() function using table

The following MySQL statement returns the column country in reverse order for those rows from the table publisher in which the column value of a country is the ‘USA’.

Code:

SELECT pub_city,country,
REVERSE(country)
FROM publisher 
WHERE country='USA';

Sample table: publisher


Output:

mysql> SELECT pub_city,country,
    -> REVERSE(country)
    -> FROM publisher 
    -> WHERE country='USA';
+----------+---------+------------------+
| pub_city | country | REVERSE(country) |
+----------+---------+------------------+
| New York | USA     | ASU              | 
| Houstan  | USA     | ASU              | 
| New York | USA     | ASU              | 
+----------+---------+------------------+
3 rows in set (0.00 sec)

Video Presentation:

All String Functions (Slides presentation)

Previous: REPLACE
Next: RIGHT



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mysql/string-functions/mysql-reverse-function.php