RIGHT Function in SQL

The RIGHT string function retrieves the characters to the given index value from the right side of the original string in the Structured Query Language.

Syntax of RIGHT String Function

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

In the syntax, we have to specify the name of that column on which we want to perform the RIGHT string function.

Syntax3: This syntax uses the RIGHT function with the string:

Examples of RIGHT String function

Example 1: The following SELECT query shows the four characters from the right side of the specified word:

Output:

Right_4_characters
OINT'

Example 2: The following SELECT query shows the right 20 characters from the given string:

Output:

Right_20_characters
NT is a good website

Example 3: The following SELECT query shows the right 16 characters from the given sentence:

Output:

Right_20_characters
CAPITAL OF INDIA

Example 4: The following SELECT query shows the 5 characters from the right of the given string:

Output:

45###

Example 5: The following SELECT query shows the 100 characters from the right of the specified string:

Output:

Right_100_characters
String Functions

Example 6: This example uses the RIGHT function with the table in Structured Query Language.

To understand the RIGHT function with SQL, we have to create the SQL table first using CREATE statement. The syntax for creating the new table in the SQL database is as follows:

The following CREATE statement creates the Worker_Info table:

The below INSERT queries insert the records of college Faculties in the Worker_Info table:

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


Worker_IdWorker_First_NameWorker_Last_NameWorker_Dept_IdWorker_AddressWorker_CityWorker_Salary
1001ArushSharma4001Aman ViharDelhi20000
1002BulbulRoy4002Nirman ViharDelhi38000
1004SaurabhRoy4001Sector 128Mumbai45000
1005ShivaniSinghania4001Vivek ViharKolkata42000
1006AvinashSharma4002Sarvodya CalonyDelhi28000
1007ShyamBesas4003Krishna NagarLucknow35000

Query 1: The following SELECT query uses the RIGHT function with the Worker_First_Name column of the above Worker_Info table:

This SQL statement shows the last three characters of the First name of each worker.

Output:

Worker_First_NameRIGHT_3FirstName
Arushush
Bulbulbul
Saurabhabh
Shivaniani
Avinashash
Shyamyam

Query 2: The following SELECT query uses the RIGHT function with the Worker_Last_Name column of the above Worker_Info table:

This SQL statement shows the last three characters of the Last name of each worker.

Output:

Worker_Last_NameRIGHT_2LastName
Sharmama
Royoy
Royoy
Singhaniaia
Sharmama
Besasas

Query 3: The following SELECT query uses the RIGHT function with the Worker_Address column of the above Worker_Info table:

This SQL statement shows the four characters from the right of the address of each worker.

Output:

Worker_AddressRIGHT_4Address
Aman Viharihar
Nirman Viharihar
Sector 128128
Vivek Viharihar
Sarvodya Calonylony
Krishna Nagaragar

Query 4: The following SELECT query uses the RIGHT function with the Worker_City column of the above Worker_Info table:

This SQL statement shows the last character of the city of each worker.

Output:

Worker_CityRIGHT_1City
Delhii
Delhii
Mumbaii
Kolkataa
Delhii
Lucknoww





Latest Courses