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.
This example returns the last 7 characters from the input string.
SELECT RIGHT('American Express', 7) AS 'Right'
Right |
---|
Express |
Syntax of the RIGHT function.
RIGHT(string, number)
string
-- a string or column name.
number
-- the number of characters to be returned.
ORDER |
---|
Id |
OrderDate |
OrderNumber |
CustomerId |
TotalAmount |
CUSTOMER |
---|
Id |
FirstName |
LastName |
City |
Country |
Phone |
SELECT FirstName, LastName,
'***' + RIGHT(OrderNumber, 3) AS 'Order Number',
TotalAmount
FROM [Order] O
JOIN Customer C ON C.Id = O.CustomerId
ORDER BY OrderNumber
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 |
CUSTOMER |
---|
Id |
FirstName |
LastName |
City |
Country |
Phone |
SELECT FirstName, LastName,
LEFT(FirstName, 3) + RIGHT(LastName, 3) AS 'Username'
FROM Customer
FirstName | LastName | Username |
---|---|---|
Paolo | Accorti | Paorti |
Pedro | Afonso | Pednso |
Marias | Anders | Marers |
Kyle | Anderson | Kylson |
Miguel | Angel Paolino | Migino |