In this step by step SQL Tutorial I will show you how to use Right() function. For the purpose of this tutorial I will use SQL Server 2012.
Other Versions of SQL Server:
- SQL Server 2012
- This blog post has been written using SQL Server 2012
- SQL Server 2008 / R2
- The examples we used should work in SQL Server 2008 and R2
- SQL Server 2005
- We suspect that this will work in SQL Server 2005 as well
Who is it for?
- People who would like to learn basics of using functions.
- Those who are preparing for Microsoft certifications and need to cover this topic.
RIGHT Function - used to take specified number of characters from a string starting from the end (right side).
RIGHT(<<String Value>>, <<Number of Characters>>)
<<String Value>> - Provide string value (field name or expression) that will be used as your input.
<<Number of Characters>> Provide number of characters that you would like to receive from the <<String Value>> you provided.
In this example I will use AdventureWorks2012 database dbo.DimCustomer table.
In this example I will show you how to use Right to return a piece of character string of email address. In first column you can see EmailAddress and the second column TopLevelDomain (last part of address like .com) you can see that i use function Right() to extract '.com' from EmailAddress column by using 4 last characters.
NOTE: In results below this function works however for non .com address this may produce incorrect results and genereally right is combined with other SQL Functions.
I hope that help