Javatpoint Logo
Javatpoint Logo

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_ID First_Name Last_Name First_City Second_City New_City Attendance_Remarks Work_Remarks Grade
10 Ramesh Sharma Lucknow Aurangabad Ghaziabad 88 95 A2
02 Yadu Sharma Aurangabad Ghaziabad Ghaziabad 95 82 A1
07 Vijay Ramna Noida Ghaziabad Lucknow 91 95 A1
04 Bhanu Rangopalr Ghaziabad Noida Lucknow 85 82 A2
11 Harry Roy Noida Kanpur Ghaziabad 95 97 A1
16 Akash Ramna Ghaziabad Meerut Aurangabad 95 90 B1
19 Ram Ramna Lucknow Ghaziabad Aurangabad 89 95 A2

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_Name REVERSE_FirstName
Ramesh hsemaR
Yadu udaY
Vijay yajiV
Bhanu unahB
Harry yrraH
Akash hsakA
Ram maR

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_Name REVERSE_LastName
Sharma amrahS
Sharma amrahS
Ramna anmaR
Rangopalr rlapognaR
Roy yoR
Ramna anmaR
Ramna anmaR

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_City REVERSE_FirstCity New_City REVERSE_NewCity
Lucknow wonkcuL Ghaziabad dabaizahG
Aurangabad dabagnaruA Ghaziabad dabaizahG
Noida adioN Lucknow wonkcuL
Ghaziabad dabaizahG Lucknow wonkcuL
Noida adioN Ghaziabad dabaizahG
Ghaziabad dabaizahG Aurangabad dabagnaruA
Lucknow wonkcuL Aurangabad dabagnaruA






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA