SQL Server RIGHT() Function
The SQL Server (Transact-SQL) RIGHT() function is used to extract a substring from a string, starting from the right-most character.
Syntax
RIGHT(string, number_of_chars)
Parameters
string |
Required. Specify the string to extract from. |
number_of_chars |
Required. Specify the number of characters to extract. If this parameter exceeds the length of the string, this function will return string. |
Return Value
Returns the substring extracted from specified string.
Example 1:
The example below shows the usage of RIGHT() function.
SELECT RIGHT('AlphaCodingSkills.com', 1); Result: 'm' SELECT RIGHT('AlphaCodingSkills.com', 4); Result: '.com' SELECT RIGHT('AlphaCodingSkills.com', 21); Result: 'AlphaCodingSkills.com' SELECT RIGHT('AlphaCodingSkills.com', 50); Result: 'AlphaCodingSkills.com' SELECT RIGHT('Alpha Coding Skills', 6); Result: 'Skills'
Example 2:
Consider a database table called Employee with the following records:
PhoneNumber | EmpID | Address |
---|---|---|
+33-147996101 | 1 | Grenelle, Paris, France |
+31-201150319 | 2 | Geuzenveld, Amsterdam, Netherlands |
+86-1099732458 | 3 | Yizhuangzhen, Beijing, China |
+65-67234824 | 4 | Yishun, Singapore |
+81-357799072 | 5 | Koto City, Tokyo, Japan |
In the query below, the RIGHT() function is used to exclude the country code from the PhoneNumber column records.
SELECT *, RIGHT(PhoneNumber, LEN(PhoneNumber) - 4) AS ContactNumber FROM Employee;
This will produce the result as shown below:
PhoneNumber | EmpID | Address | ContactNumber |
---|---|---|---|
+33-147996101 | 1 | Grenelle, Paris, France | 147996101 |
+31-201150319 | 2 | Geuzenveld, Amsterdam, Netherlands | 201150319 |
+86-1099732458 | 3 | Yizhuangzhen, Beijing, China | 1099732458 |
+65-67234824 | 4 | Yishun, Singapore | 67234824 |
+81-357799072 | 5 | Koto City, Tokyo, Japan | 357799072 |
❮ SQL Server Functions