Home Functions RIGHT()
SQL String Function

RIGHT()

RIGHT() returns the rightmost n characters of a string, counting from the end. It is the mirror image of LEFT().

MySQLPostgreSQLSQL Server
Returns: A string containing the rightmost n characters of the input. Returns NULL when the input is NULL.

Syntax

RIGHT(string, n)
ParameterTypeRequiredDescription
string string expression yes The text to take characters from. Non string values are usually cast to text first.
n integer yes How many characters to return from the end of the string. If n is greater than the length, the whole string is returned.

How it works

RIGHT(string, n) extracts a fixed number of characters from the end of a string. RIGHT('payment', 4) returns ment. It is the direct counterpart of LEFT(), which takes characters from the start instead.

Anything RIGHT() can do is also expressible with SUBSTRING() using a computed start position. RIGHT(str, n) is equivalent to SUBSTRING(str, LENGTH(str) - n + 1, n): you start n characters before the end and take the rest. RIGHT() is simply the shorter, clearer way to write that common case.

One portability note: SQLite has no RIGHT() function. There you use SUBSTR() with a negative start index, as in SUBSTR(str, -4), which counts back from the end and returns the last 4 characters. MySQL, PostgreSQL and SQL Server all provide RIGHT() natively.

Examples

Last four digits of a card number

-- Never store or show full card numbers; the last 4 is enough to identify one
SELECT RIGHT(card_number, 4) AS last4
FROM payment_methods;
Result
last4
-----
4242
0005
1881

Pull the file extension

-- The last 3 characters of common uploads
SELECT filename, RIGHT(filename, 3) AS ext
FROM uploads;
Result
filename    | ext
------------+----
 report.pdf | pdf
 photo.jpg  | jpg
 data.csv   | csv

Last N characters of any text

SELECT sku, RIGHT(sku, 5) AS tail
FROM products;
Result
sku          | tail
-------------+------
 AB-2026-991 | 6-991
 CD-2026-004 | 6-004

Mask an account number, showing only the end

-- Replace everything but the final 4 characters with asterisks
SELECT
  CONCAT('****', RIGHT(account_no, 4)) AS masked
FROM accounts;
Result
masked
----------
****9931
****0027

RIGHT rewritten with SUBSTRING

-- These two columns return the same value
SELECT
  RIGHT(email, 4)                                  AS with_right,
  SUBSTRING(email, CHAR_LENGTH(email) - 4 + 1, 4)  AS with_substring
FROM users;
Result
with_right | with_substring
-----------+---------------
 .com      | .com
 .org      | .org

Common mistakes

Wrong
-- Expecting an error or a padded value when n is too big
-- 'ab' has length 2, so RIGHT('ab', 5) does NOT return 5 chars
SELECT RIGHT('ab', 5);  -- returns 'ab', not '   ab'
Right
-- If you need a fixed width, pad first, then take the right side
SELECT RIGHT(CONCAT('     ', 'ab'), 5);  -- '   ab'

When n is larger than the string length, RIGHT() returns the whole string unchanged. It never errors and never pads. Use LPAD or CONCAT first if you need a guaranteed length.

Wrong
-- SQLite has no RIGHT() function; this raises an error there
SELECT RIGHT(phone, 4) FROM contacts;
Right
-- In SQLite, use SUBSTR with a negative start index
SELECT SUBSTR(phone, -4) FROM contacts;

MySQL, PostgreSQL and SQL Server ship RIGHT(), but SQLite does not. On SQLite use SUBSTR(str, -n), where the negative start counts back from the end of the string.

Wrong
-- Wanting the FIRST characters but reaching for RIGHT
SELECT RIGHT(country_code, 2) AS prefix
FROM addresses;  -- gives the LAST 2, not the first 2
Right
-- Use LEFT() when you want characters from the start
SELECT LEFT(country_code, 2) AS prefix
FROM addresses;

It is easy to confuse the two. RIGHT() takes characters from the end; LEFT() takes them from the start. Pick the one that matches the side you mean.

Performance

RIGHT() is a cheap, per row string operation, so it costs almost nothing on the values it touches. The real cost comes when you use it in a WHERE clause. A predicate like WHERE RIGHT(email, 4) = '.com' wraps the column in a function, which prevents the optimiser from using a plain index on that column and forces a full scan.

If you frequently filter or group by the tail of a string, store that suffix in its own column or build an expression based index (a functional index in PostgreSQL, a computed persisted column in SQL Server) on RIGHT(col, n). See the indexing guide for how functional indexes let these lookups stay fast.

Interview questions

What does RIGHT(string, n) return?

It returns the rightmost n characters of the string, counting from the end. RIGHT('invoice', 3) returns ice. It is the mirror of LEFT().

How is RIGHT() related to SUBSTRING()?

They are interchangeable. RIGHT(str, n) equals SUBSTRING(str, LENGTH(str) - n + 1, n): it starts n characters before the end and returns the rest. RIGHT() is just a convenient shorthand for that computed start.

SELECT SUBSTRING(str, CHAR_LENGTH(str) - 4 + 1, 4) AS same_as_right4
FROM t;

What happens when n is larger than the length of the string?

The whole string is returned unchanged. RIGHT() does not raise an error and does not pad the value to length n. If you need a fixed width, pad the string before calling RIGHT().

How do you get the last n characters in SQLite, which has no RIGHT()?

Use SUBSTR(str, -n). A negative start index counts back from the end, so SUBSTR(str, -4) returns the last 4 characters, matching RIGHT(str, 4) on other engines.

How would you mask a value so only the last four characters are visible?

Concatenate a fixed mask with the tail, for example CONCAT('****', RIGHT(account_no, 4)). This is a common pattern for card numbers, phone numbers and account identifiers.

Master SQL, one function at a time

Browse the full SQL functions library, or learn the fundamentals with our free, structured courses.