REVERSE String Function in SQL

The REVERSE string function in SQL returns the string in the reverse order. It shows the last character of the string at the first position and the first character at the last position in the output of the query.

Syntax of REVERSE String Function

Syntax1: This syntax uses the REVERSE function with the column name of the SQL table:

In the syntax, we have to specify the name of that column on which the REVERSE function is to be run.

Syntax2: This syntax uses the REVERSE function with the string:

Examples of REVERSE String function

Example 1: The following SELECT query reverses the given string:

Output:

'TNIOPTAVAJ          '

Example 2: The following SELECT query shows the given string in the reverse order:

Output:

'AIDNI FO LATIPAC EHT SI IHLED WEN'

Example 3: The following SELECT query shows the numeric string in the reverse way:

Output:

524792244875

Example 4: This example uses the REVERSE function with the table in Structured Query Language.

First, we have to create the new SQL table, which helps to understand the REVERSE string function. The syntax for creating the new table in the SQL database is as follows:

The following CREATE statement creates the Employee_Grade table:

The below INSERT queries insert the records of Employees with Grades and Remarks in the Employee_Grade table:

The following SELECT statement displays the inserted records of the above Employee_Grade table:


Employee_IDFirst_NameLast_NameFirst_CitySecond_CityNew_CityAttendance_RemarksWork_RemarksGrade
10RameshSharmaLucknowAurangabadGhaziabad8895A2
02YaduSharmaAurangabadGhaziabadGhaziabad9582A1
07VijayRamnaNoidaGhaziabadLucknow9195A1
04BhanuRangopalrGhaziabadNoidaLucknow8582A2
11HarryRoyNoidaKanpurGhaziabad9597A1
16AkashRamnaGhaziabadMeerutAurangabad9590B1
19RamRamnaLucknowGhaziabadAurangabad8995A2

Query 1: The following SELECT query uses the REVERSE function with the First_Name column of the above Employee_Grade table:

This statement shows all the values of the first name in reverse order.

Output:

First_NameREVERSE_FirstName
RameshhsemaR
YaduudaY
VijayyajiV
BhanuunahB
HarryyrraH
AkashhsakA
RammaR

Query 2: The following SELECT query uses the REVERSE function with the Last_Name column of the above Employee_Grade table:

This statement shows all the values of the last name in reverse order.

Output:

Last_NameREVERSE_LastName
SharmaamrahS
SharmaamrahS
RamnaanmaR
RangopalrrlapognaR
RoyyoR
RamnaanmaR
RamnaanmaR

Query 3: The following SELECT query uses the REVERSE function with the First_City and New_City columns of the above Employee_Grade table:

This SQL statement shows all the cities of First_City and New_City in the reverse order.

Output:

First_CityREVERSE_FirstCityNew_CityREVERSE_NewCity
LucknowwonkcuLGhaziabaddabaizahG
AurangabaddabagnaruAGhaziabaddabaizahG
NoidaadioNLucknowwonkcuL
GhaziabaddabaizahGLucknowwonkcuL
NoidaadioNGhaziabaddabaizahG
GhaziabaddabaizahGAurangabaddabagnaruA
LucknowwonkcuLAurangabaddabagnaruA





Latest Courses