How to Reverse a String in MySQL
In this tutorial, we are going to see how to reverse a string in MySQL. In SQL language, REVERSE() function allows you to return a string by reversing the order of the characters. The first character becomes the last one, the second one becomes the second last one, and so on.
Note: the function handles multi-byte characters such as “œ”.
Syntax:
The function is used in an SQL query using the following syntax:
SELECT REVERSE(str);
In this example, the “str” parameter refers to a string.
Example :
The SQL function REVERSE() is used to reverse the order of characters. These few examples illustrate the use of the REVERSE() function:
SELECT REVERSE('abc'); -- output: cba SELECT REVERSE('123'); -- output: 321 SELECT REVERSE('hello world'); -- output: dlrow olleh
The output string of these SQL queries shows that the order of the letters is indeed reversed between the input string and the output string.