Skip to content

Latest commit

 

History

History
110 lines (87 loc) · 4.02 KB

reverse-transact-sql.md

File metadata and controls

110 lines (87 loc) · 4.02 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic f1_keywords helpviewer_keywords dev_langs monikerRange
REVERSE (Transact-SQL)
REVERSE (Transact-SQL)
MikeRayMSFT
mikeray
03/13/2017
sql
t-sql
reference
REVERSE_TSQL
REVERSE
expressions [SQL Server], reverse
REVERSE function
reverse character expressions
TSQL
>= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || >= sql-server-linux-2017 || = azuresqldb-mi-current||=fabric

REVERSE (Transact-SQL)

[!INCLUDE sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw]

Returns the reverse order of a string value.

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

Syntax

REVERSE ( string_expression )  

Arguments

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

Remarks

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.

Examples

The following example returns all contact first names with the characters reversed. This example uses the [!INCLUDEssSampleDBobject] database.

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

[!INCLUDEssResult]

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  

Examples: [!INCLUDEssazuresynapse-md] and [!INCLUDEssPDW]

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

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

See Also

CONCAT (Transact-SQL)
CONCAT_WS (Transact-SQL)
FORMATMESSAGE (Transact-SQL)
QUOTENAME (Transact-SQL)
REPLACE (Transact-SQL)
STRING_AGG (Transact-SQL)
STRING_ESCAPE (Transact-SQL)
STUFF (Transact-SQL)
TRANSLATE (Transact-SQL)
CAST and CONVERT (Transact-SQL)
Data Types (Transact-SQL)
String Functions (Transact-SQL)