SQL RIGHT Function

RIGHT returns the right part from a string, given a number of characters.

The number of characters must be a positive number, or an error will occur.

Example

#

This example returns the last 7 characters from the input string.

SELECT RIGHT('American Express', 7) AS 'Right'
Result:  1 record
Right
Express

Syntax

Syntax of the RIGHT function.

RIGHT(string, number)

string -- a string or column name.

number -- the number of characters to be returned.


More Examples

RIGHT with Column

ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount
CUSTOMER
Id
FirstName
LastName
City
Country
Phone
Problem: List orders with only the last 3 characters of the OrderNumber displayed.
SELECT FirstName, LastName,
       '***' + RIGHT(OrderNumber, 3) AS 'Order Number',
       TotalAmount
  FROM [Order] O 
  JOIN Customer C ON C.Id = O.CustomerId
 ORDER BY OrderNumber
Result:  830 records
FirstName LastName Order Number TotalAmount
Paul Henriot ***378 440.00
Karin Josephs ***379 1863.40
Mario Pontes ***380 1813.00
Mary Saveley ***381 670.80
Pascale Cartrain ***382 3730.00

RIGHT with LEFT

CUSTOMER
Id
FirstName
LastName
City
Country
Phone
Problem: List customers with usernames created by the first 3 characters of the first name and the last 3 characters of their last name.
SELECT FirstName, LastName,
       LEFT(FirstName, 3) + RIGHT(LastName, 3) AS 'Username'
  FROM Customer
Result:  91 records
FirstName LastName Username
Paolo Accorti Paorti
Pedro Afonso Pednso
Marias Anders Marers
Kyle Anderson Kylson
Miguel Angel Paolino Migino

You may also like



Guides


vsn 3.1