The REVERSE() function in SQL Server flips the order of characters in a string, reversing it from back to front.
Syntax:
REVERSE ( string_expression )
string_expression: The string or text expression that you want to reverse.
E.g.:
SELECT REVERSE('SQL SERVER') AS Result
For a more complex example, you can use REVERSE() to find the last 5 characters of a string. For instance, suppose we have an invoice number like INV-2026-00125
DECLARE @DocumentNo VARCHAR(50) = 'INV-2026-00125'
SELECT LEFT(REVERSE(@DocumentNo), 5) AS Result
However, because the result comes out backwards, we need to reverse it one more time to restore the normal order.
DECLARE @DocumentNo VARCHAR(50) = 'INV-2026-00125'
SELECT REVERSE(LEFT(REVERSE(@DocumentNo), 5)) AS Result



0 Comments