| title | REVERSE (Transact-SQL) | Microsoft Docs | |||
|---|---|---|---|---|
| ms.custom | ||||
| ms.date | 03/13/2017 | |||
| ms.prod | sql-non-specified | |||
| ms.reviewer | ||||
| ms.suite | ||||
| ms.technology |
|
|||
| ms.tgt_pltfrm | ||||
| ms.topic | language-reference | |||
| f1_keywords |
|
|||
| dev_langs |
|
|||
| helpviewer_keywords |
|
|||
| ms.assetid | 555d8877-7cc7-4955-ae2c-6215aca313b7 | |||
| caps.latest.revision | 46 | |||
| author | BYHAM | |||
| ms.author | rickbyh | |||
| manager | jhubbard |
[!INCLUDEtsql-appliesto-ss2008-all_md]
Returns the reverse order of a string value.
Transact-SQL Syntax Conventions
-- Syntax for SQL Server, Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse
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.
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 [!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
The following example returns names of all databases, and the names with the characters reversed.
SELECT name, REVERSE(name) FROM sys.databases;
GO
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
CAST and CONVERT (Transact-SQL)
Data Types (Transact-SQL)
String Functions (Transact-SQL)