RIGHT functions in SQL
The RIGHT() function in SQL is used to extract a specified number of characters from the right side of a string.
SQL Syntax
RIGHT(string, number_of_characters)
- string: The input string from which characters will be extracted.
- number_of_characters: The number of characters you want to extract from the right side of the string.
Example: Let's we have a table Employees.
Table: Employees
| ID | Name |
|---|---|
| 1 | Ashish |
| 2 | Priya |
| 3 | Rahul |
| 4 | Sneha |
Now we want to extract the last 3 characters from each employee's name.
SQL
SELECT RIGHT(name, 3) AS ShortName FROM employees;
If the name is "Ashish", the result will be "ish".
The result of the above query is shown below:
| ID | Name | ShortName |
|---|---|---|
| 1 | Ashish | ish |
| 2 | Priya | iya |
| 3 | Rahul | hul |
| 4 | Sneha | eha |