If you ever need to reverse the order of a string value in MySQL – that is, return a string with the order of the characters reversed – the REVERSE() function is what you need.
Syntax
Here’s how it works:
REVERSE(str)
Where
str
is the string you want reversed.Example
Here’s an example you can copy and paste into MySQL and change the values to suit:
SELECT REVERSE('Cat');
Result:
+----------------+ | REVERSE('Cat') | +----------------+ | taC | +----------------+
Multiple Words
Note that the whole string is reversed (not just each word within a string). So in a string with many words, the first word will come last and vice-versa.
Example:
SELECT REVERSE('plug snub leg') AS Result;
Result:
+---------------+ | Result | +---------------+ | gel buns gulp | +---------------+
A Database Query Example
Here’s an example of using
REVERSE()
on values returned in a database query:USE Music; SELECT ArtistName, REVERSE(ArtistName) AS Reversed FROM Artists LIMIT 5;
Result:
+------------------+------------------+ | ArtistName | Reversed | +------------------+------------------+ | Iron Maiden | nediaM norI | | AC/DC | CD/CA | | Allan Holdsworth | htrowsdloH nallA | | Buddy Rich | hciR ydduB | | Devin Townsend | dnesnwoT niveD | +------------------+------------------+
Thank you for sharing this informative post. Looking forward to reading more.
ReplyDeleteWeb Design and Development Company in Delhi/NCR