Javatpoint Logo
Javatpoint Logo

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_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Address Worker_City Worker_Salary
1001 Arush Sharma 4001 Aman Vihar Delhi 20000
1002 Bulbul Roy 4002 Nirman Vihar Delhi 38000
1004 Saurabh Roy 4001 Sector 128 Mumbai 45000
1005 Shivani Singhania 4001 Vivek Vihar Kolkata 42000
1006 Avinash Sharma 4002 Sarvodya Calony Delhi 28000
1007 Shyam Besas 4003 Krishna Nagar Lucknow 35000

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_Name RIGHT_3FirstName
Arush ush
Bulbul bul
Saurabh abh
Shivani ani
Avinash ash
Shyam yam

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_Name RIGHT_2LastName
Sharma ma
Roy oy
Roy oy
Singhania ia
Sharma ma
Besas as

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_Address RIGHT_4Address
Aman Vihar ihar
Nirman Vihar ihar
Sector 128 128
Vivek Vihar ihar
Sarvodya Calony lony
Krishna Nagar agar

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_City RIGHT_1City
Delhi i
Delhi i
Mumbai i
Kolkata a
Delhi i
Lucknow w






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