MySQL REVERSE() Function
MySQL REVERSE() Function
The REVERSE() function returns a string with all of its characters arranged in backward order. For non-binary utf8mb4 strings, MySQL reverses logical characters safely without scrambling multibyte UTF-8 sequences.
Syntax
REVERSE(str)
- str: The string expression or column to reverse.
- Return value: Reversed string,
''for'', orNULLwhenstrisNULL.
Because REVERSE() operates on characters for text types, REVERSE('café') produces 'éfac' cleanly.
Practical Example
Suppose demo_orders contains tracking_number strings of varying lengths, and you want to extract or inspect the last token before a hyphen or build a reversed checksum view.
SELECT
order_id,
tracking_number,
REVERSE(tracking_number) AS reversed_tracking,
REVERSE(SUBSTRING_INDEX(REVERSE(tracking_number), '-', 1)) AS last_segment
FROM demo_orders
ORDER BY order_id;
| order_id | tracking_number | reversed_tracking | last_segment |
|---|---|---|---|
| 501 | TRK-2026-991 | 199-6202-KRT | 991 |
| 502 | AIR-88-PL | LP-88-RIA | PL |
| 503 | NULL | NULL | NULL |
Note that SUBSTRING_INDEX(tracking_number, '-', -1) extracts '991' more directly, though REVERSE() helps when pairing with LOCATE() from the right edge.
Common Mistakes & NULL Behavior
- Using REVERSE() on BINARY columns: On
BINARYorVARBINARYdata,REVERSE()flips raw bytes. Reversing raw UTF-8 bytes stored inside aBLOBcorrupts multibyte characters. - Reversing combining diacritics: If a character uses a base letter plus a separate combining accent code point, reversing swaps their order and shifts the accent onto the neighbor.
- Indexing reversed domains: Some teams index
REVERSE(email)to speed up suffix searches like@gmail.comusing a standard B-tree prefix scan (LIKE REVERSE('%@gmail.com')). - NULL input:
REVERSE(NULL)returnsNULL.
Frequently Asked Questions
How can I check if a string is a palindrome in MySQL?
Compare WHERE code = REVERSE(code). Under default _ci collations, 'Level' matches 'leveL' unless you compare with BINARY.
Does REVERSE() trim trailing spaces before flipping?
No. Trailing spaces become leading spaces in the reversed output (REVERSE('abc ') yields ' cba').
Is REVERSE() available in SQLite as well?
No. Standard SQLite does not include a built-in REVERSE() function, whereas MySQL supports it natively.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.