RIGHT Function - SQL


Overview


The RIGHT function in SQL returns a specified number of characters from the end of a string. It is useful for extracting a portion of a string from the right side.

Example:

SELECT RIGHT('Astronomy', 5)

Syntax:

SELECT RIGHT(string, num_of_characters)

string is the string from which you want to extract characters.

num_of_characters is the number of characters to extract from the end of the string.

Sample Data:

department_id first_name
3 Frank
2 Jane
3 Ashley
NULL Glenn
2 Kelly
1 Richard
1 George
5 Kyle
2 James
1 Gustavo

Example: Hard-coded value


In this example, we are returning the 5 characters to the right of the string ‘astronomy,’ the final output is ‘onomy.’


Example: Query without the WHERE Statement


In this example, we are returning the 3 characters to the right of all of the rows in the first_name column.


Example: Query with the WHERE Statement


In this example, we are returning the 3 characters to the right of all of the rows in the first_name column where the department_id equals 1.