REVERSE (Transact-SQL)

Returns the reversed character string that is supplied as input.

REVERSE ( string_expression )

Parameters

Return Value

Returns Varchar or Nvarchar.

Remarks

The REVERSE function reverses the order of characters in the input string expression. For example, "ABC" becomes "CBA".

The data type of the returned value depends on the data type of the input string_expression.

REVERSE can be useful in various scenarios, such as data validation or manipulating text data.

Examples

Example 1: Reversing a literal string

SELECT REVERSE('SQL Server');
GO

Result:

.revreS LQS

Example 2: Reversing a variable

DECLARE @MyString VARCHAR(50) = 'Transact-SQL';
SELECT REVERSE(@MyString);
GO

Result:

LQS-tca_snarT

Example 3: Reversing a column from a table

Assume a table named Products with a column ProductName.

SELECT ProductName, REVERSE(ProductName) AS ReversedName
FROM Products
WHERE ProductID = 1;
GO

If ProductName for ProductID 1 is 'Chai', the output might look like:

ProductName ReversedName
Chai iahC

See Also

System Functions (Transact-SQL)

String Functions (Transact-SQL)