REVERSE (Transact-SQL)

THIS TOPIC APPLIES TO: yesSQL Server (starting with 2008)yesAzure SQL DatabaseyesAzure SQL Data Warehouse yesParallel Data Warehouse

Returns the reverse order of a string value.

Topic link icon Transact-SQL Syntax Conventions


REVERSE ( string_expression )  


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

Return Types

varchar or nvarchar


string_expression must be of a data type that is implicitly convertible to varchar. Otherwise, use CAST to explicitly convert string_expression.

Supplementary Characters (Surrogate Pairs)

When using SC collations, the REVERSE function will not reverse the order of two halves of a surrogate pair.


The following example returns all contact first names with the characters reversed. This example uses the AdventureWorks2012 database.

SELECT FirstName, REVERSE(FirstName) AS Reverse  
FROM Person.Person  
WHERE BusinessEntityID < 5  
ORDER BY FirstName;  

Here is the result set.

FirstName      Reverse
-------------- --------------
Ken            neK
Rob            boR
Roberto        otreboR
Terri          irreT

(4 row(s) affected)

The following example reverses the characters in a variable.

DECLARE @myvar varchar(10);  
SET @myvar = 'sdrawkcaB';  
SELECT REVERSE(@myvar) AS Reversed ;  

The following example makes an implicit conversion from an int data type into varchar data type and then reverses the result.

SELECT REVERSE(1234) AS Reversed ;  

Examples: Azure SQL Data Warehouse and Parallel Data Warehouse

The following example returns names of all databases, and the names with the characters reversed.

SELECT name, REVERSE(name) FROM sys.databases;  

See Also

CAST and CONVERT (Transact-SQL)
Data Types (Transact-SQL)
String Functions (Transact-SQL)