Home > SQL Server > SQL SERVER – REVERSE() a string function @SQLSERVER

SQL SERVER – REVERSE() a string function @SQLSERVER


REVERSE() : It is a string function used to change the position of the whole string in the reverse order. This function takes a string or binary argument value. But, it converts the argument to string data type implicitly otherwise we have to use CAST or CONVERT function to change the function argument type to string. It returns varchar or nvarchar type value as a function output.

Usage

REVERSE ( string_expression )

Example

1. Simple example to show how Reverse function uses string constant

select ‘SQL SERVER’ as ‘Original value’, Reverse(‘SQL SERVER’) as ‘Reversed String’

Output

pic1

2. This example is to show you, How reverse function is converting its arguments DATE and FLOAT to a string value implicitly.

pic23. In this example I am going to use the REVERSE function to find a string is Palindrome or not.

Before we begin, let us learn about the history behind Palindrome

A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction.

The word “palindrome” was coined from the Greek by the English writer Ben Jonson in the 17th century. There are more interesting stuff in that page. So, please read more information about Palindrome here – Wikipedia

SQL Query

Declare @str as varchar(50)
Set @str = ‘SQL SERVER RIDER’
Select IIF( @str = REVERSE(@str),  @str+’ is a Palindrome’, @str+’ is not a Palindrome’) as ‘Finding palindrome Word’

Output 1

pic3Output 2

pic4A list of single or multiple word Palindrome.

I hope you have understood the user of REVERSE function. To learn about other string function please refer to the blog post “String Functions at a glance“.

Advertisements
  1. No comments yet.
  1. June 30, 2017 at 10:09 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: