The SQL REVERSE is one of the SQL String Function, which is used to reverse the specified expression.
In this article we will show you, How to write String REVERSE Function with an example.
Sql Server REVERSE Function Syntax
The basic syntax of the SQL Server REVERSE Function is as shown below:
SELECT REVERSE (String_Expression) FROM [Source]
We are going to use the below shown data to explain the REVERSE Function
SQL REVERSE Function Example 1
The REVERSE Function is used to reverse the given string. The following query will show multiple ways to use this String REVERSE Function.
T-SQL CODE
DECLARE @String_Expression varchar(50) SET @String_Expression = 'SQL Server' SELECT REVERSE (@String_Expression) AS 'SQLReverse' --String Reverse directly SELECT REVERSE ('SQL Server 2014') AS 'SQL Reverse'
OUTPUT
ANALYSIS
Within this reverse function example query, the below lines of code is used to declare string variable and assigning the string data.
DECLARE @String_Expression varchar(50) SET @String_Expression = 'SQL Server'
In the below statement, We used REVERSE function to reverse the string variable @String_Expression. We also assigned new name to that result using ALIAS Column.
SELECT REVERSE (@String_Expression) AS 'SQL Reverse'
In the next line, We used the REVERSE Function directly on the string.
--String Reverse directly SELECT REVERSE ('SQL Server 2014') AS 'SQL Reverse'
SQL REVERSE Function Example 2
The REVERSE function also allows you to reverse the expressions inside the columns. In this example, We are going to reverse all the records in [Department Name] column present in the Employe table.
T-SQL CODE
SELECT [FirstName] ,[LastName] ,[DepartmentName] ,REVERSE ([DepartmentName]) AS [SQL Reverse] FROM [Employe]
OUTPUT
Thank You for Visiting Our Blog