Export (0) Print
Expand All

REVERSE (Transact-SQL)

Applies To: SQL Server 2014, SQL Server 2016 Preview

Topic Status: Some information in this topic is preview and subject to change in future releases. Preview information describes new features or changes to existing features in Microsoft SQL Server 2016 Community Technology Preview 2 (CTP2).

Returns the reverse order of a string value.

Applies to: SQL Server (SQL Server 2008 through current version), Azure SQL Database, Azure SQL Data Warehouse Public Preview.

Topic link icon Transact-SQL Syntax Conventions

REVERSE ( string_expression )

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.

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;
GO

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 ;
GO

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 ;
GO
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2015 Microsoft