How to Return a String in Reverse Order using SQL Server – REVERSE()

Starting from SQL Server 2008, the REVERSE() function can be used to reverse the order of a string. That is, it returns the characters in the string in reverse order.

Here’s the syntax:

REVERSE ( string_expression )

Where string_expression is an expression of a string or binary data type. It can be a constant, variable, or column of either character or binary data.

Example

Here’s a quick example to demonstrate:

SELECT REVERSE('Dog');

Result:

goD

Multiple Words

Note that the whole string is reversed. So if it contains multiple words, the first word goes to the end, and the last word to the start, etc.

Here’s an example of what I mean:

SELECT REVERSE('faced drawer pay');

Result:

yap reward decaf

Reversing Strings from a Database Query

Here’s an example of reversing data returned from a database:

USE Music;
SELECT TOP 5 AlbumName, REVERSE(AlbumName) AS Reversed
FROM Albums;

Result:

AlbumName               Reversed              
----------------------  ----------------------
Powerslave              evalsrewoP            
Powerage                egarewoP              
Singing Down the Lane   enaL eht nwoD gnigniS 
Ziltoid the Omniscient  tneicsinmO eht diotliZ
Casualties of Cool      looC fo seitlausaC