Skip Navigation Links
Home
Functions
Tips & Tricks
SQL Server 2005
SQL Server 2008
SQL Server 2012
FAQ
Forums
Practice Test
Bookstore
Tip of the Day : Example Uses of the REVERSE String Function
Welcome to SQL Server Helper!!!

This site is intended for those who are beginning to use SQL Server as part of their day-to-day activities.  You will find in this site a collection of useful functions, triggers, stored procedures and tips and tricks related to SQL Server.

Should you have any comments or questions regarding this site or if you want to ask SQL Server-related questions, e-mail us here.

We hope we are able to help and we are glad to help!!!

 
SQL Server Tip of the Day - September 23, 2014
Example Uses of the REVERSE String Function

The REVERSE string function returns the reverse of a string value. The syntax of the REVERSE string function is as follows:

REVERSE (  )

The <string_expression> parameter of the REVERSE string function is an expression of a string or binary data type which can be a constant, variable or column of either character or binary data. It must be of a data type that is implicitly convertible to VARCHAR data type.

Here are a few examples of the uses of the REVERSE string function:

Usage #1 : Determine if a String is a Palindrome

DECLARE @Input VARCHAR(100) = 'NEVER ODD OR EVEN'

SELECT CASE WHEN REPLACE(@Input, ' ', '') = REVERSE(REPLACE(@Input, ' ', ''))
            THEN 'Palindrome'
            ELSE 'Not a Palindrome' END AS [IsPalindrome]
GO

IsPalindrome
-------------
Palindrome

Usage #2 : Get the Last Name from a Full Name

DECLARE @FullName VARCHAR(100) = 'Bill Gates'

SELECT REVERSE(LEFT(REVERSE(@FullName), CHARINDEX(' ', REVERSE(@FullName)) - 1)) AS [LastName]
SELECT RIGHT(@FullName, CHARINDEX(' ', REVERSE(@FullName)) - 1) AS [LastName]

LastName
----------
Gates

Usage #3 : Get the Page Name from a URL

DECLARE @URL VARCHAR(100) = 'http://www.sql-server-helper.com/default.aspx'

SELECT REVERSE(LEFT(REVERSE(@URL), CHARINDEX('/', REVERSE(@URL)) - 1)) AS [PageName]
SELECT RIGHT(@URL, CHARINDEX('/', REVERSE(@URL)) - 1) AS [PageName]

PageName
-----------
default.aspx

Usage #4 : Get the File Name from a Full Path

DECLARE @FullFilePath VARCHAR(100)
SET @FullFilePath = 'C:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Binn\sqlservr.exe'

SELECT REVERSE(LEFT(REVERSE(@FullFilePath), 
                    CHARINDEX('\', REVERSE(@FullFilePath)) - 1)) AS [FileName]
SELECT RIGHT(@FullFilePath, CHARINDEX('\', REVERSE(@FullFilePath)) - 1) AS [FileName]

FileName
----------
sqlservr.exe

Usage #5 : Get the Last Word from a Sentence / String

DECLARE @Sentence VARCHAR(100) = 'To be or not to be, that is the question'

SELECT REVERSE(LEFT(REVERSE(@Sentence), CHARINDEX(' ', REVERSE(@Sentence)) - 1)) AS [LastWord]

LastWord
----------
question
 
SQL Server 2012
What's New in SQL Server 2012
SQL Server 2012 Articles
 
SQL Server 2008
What's New in SQL Server 2008

A collection of articles related to what's new with SQL Server 2008:

SQL Server 2008 Articles

A collection of articles related to SQL Server 2008:

 
User-Defined Functions
Date Functions

A collection of useful user-defined functions that deal with dates.

String Functions

A collection of useful user-defined functions that deal with strings (varchar/char/nvarchar/nchar).

Tree Functions

A collection of useful user-defined functions that deal with tree or hierarchical design structures.

Table-Valued Functions
A collection of useful table-valued user-defined functions that can be used to join with other tables.
SQL Server Built-in Functions
A reference to all built-in functions available within SQL Server grouped into categories.
 
Tips and Tricks

A collection of useful SQL Server-related tips and tricks:

SQL Server Error Messages

A list of SQL Server error messages and for certain error messages, discusses ways on how to solve the error or work around them:

 
Frequently Asked Questions
SQL Server

A collection of frequently asked questions about SQL Server grouped by categories:

SQL Server 2005

A collection of frequently asked questions about SQL Server 2005 grouped by categories: