RIGHT function is used to get right part of the specified string with the specified number of characters.
Syntax of RIGHT Function :
RIGHT ( character_expression ,integer_expression )
character_expression is an expression of character or binary data. character_expression can be a constant, variable, or column. character_expression can be of any data type except text or ntext.
integer_expression is a positive integer that specifies how many characters of the character_expression will be returned. It returns an error if integer_expression is negative.
Return type of RIGHT function is varchar or nvarchar.
Examples of RIGHT Function :
Example 1 : Use of RIGHT function in select clause
SELECT RIGHT('SYNTAX-EXAMPLE',7)
Output
EXAMPLE
Above example returns rightmost 7 characters of specified string.
Example 2 : Use of RIGHT function to display rightmost 5 characters of table field.
SELECT RIGHT(ShipName,5)
FROM Orders
Output
alier
täten
arnes
stock
lices
arnes
Above example displays last 5 characters from field ShipName of orders table.
Example 3 : Use of RIGHT function in where clause
SELECT DISTINCT ShipName
FROM Orders
WHERE RIGHT(ShipName,2) = 'on'
Output
Eastern Connection
France restauration
Above example displays unique ship name ending with characters 'on' in order table.