Knowledge base / SQL

SQL RIGHT: text suffixes and fixed-length assumptions

I show correct suffix results, dialect differences and why the last characters are not automatically a domain or a surname.

Kajo RudzińskiPublished Updated 5 min read

A frame selects the final three blocks from a long sequence.

RIGHT extracts a specified number of characters from the end of a string. I use it for codes with a known structure, such as a three-character product-series suffix. First, though, I establish that the suffix length is really fixed. The function does not independently recognize domains, surnames or file extensions.

These examples are synthetic. The main syntax targets PostgreSQL, with comparisons to MySQL 8.4, SQL Server and alternatives in Oracle 19c and SQLite. I checked the syntax and boundaries in official documentation on 22 November 2026.

Syntax and the first result

In PostgreSQL, MySQL and SQL Server, the basic form is RIGHT(text, n). For positive n it selects the final characters in their original order. The result remains text; the function does not reverse the letters.

SELECT RIGHT('ABC123XYZ', 3) AS suffix;

The result is XYZ. Counting from the end finds Z, Y and X, but the output preserves X, Y, Z. If an identifier contains significant leading zeros, retain it as text. Converting it to a number before extraction can already remove parts of its representation that matter to interpreting the code.

Four characters means four characters

The previous Polish example supplied an incorrectly shortened result. In PostgreSQL, this expression returns ęga!, rather than ga!. Starting from the right selects the exclamation mark, a, g and ę, then retains their order from the original string.

SELECT RIGHT('SQL to potęga!', 4) AS suffix;

For the Polish literal in SQL Server I use Unicode syntax: RIGHT(N'SQL to potęga!', 4). Its documentation also explains how SC collations count UTF-16 surrogate pairs. I therefore avoid equating every visible symbol with one byte or promising identical segmentation for every complex Unicode representation.

The character count is straightforward for this small example. A byte-width export needs a separate encoding specification. RIGHT operating on text does not replace validation of a binary file’s field size. The CHAR_LENGTH entry discusses the distinction between character and byte lengths in more detail.

Extract from a column while retaining the source

Suppose products contains the codes ABC123XYZ and DEF456UVW. In this exercise their final three characters identify a series. I keep the full code beside its extracted suffix so that the result can be inspected and explained.

SELECT product_code, RIGHT(product_code, 3) AS series
FROM products
ORDER BY product_code;

The suffixes are XYZ and UVW respectively. Retaining the source column is especially helpful during an initial data review. A short or malformed code can still produce a convincing-looking suffix. RIGHT does not establish that the complete identifier has the correct length or that the extracted series exists in a reference list.

Domains have variable lengths

The string example.com has eleven characters. For ada@example.com, RIGHT with 10 returns xample.com; with 11 it returns example.com. Correcting the count fixes that single example, but it does not provide a general domain extractor. A domain ending in example.org.pl has a different length.

For controlled input in which every address has exactly one @ character, PostgreSQL’s SPLIT_PART can select the second part. This demonstrates separator-based parsing, not validation of arbitrary email addresses. Missing or repeated separators need their own handling rule.

SELECT split_part('ada@example.com', '@', 2) AS domain;

The result is example.com. Similarly, three trailing characters do not identify every file extension, and the last word of a person’s full name is not a reliable surname model. I remove broad promises about those use cases. First define the structure and exceptions, then choose an extraction function that matches that definition.

Short and long suffixes appear after separators in two different sequences.

Lengths and missing values

In PostgreSQL, RIGHT('abc', 8) returns the complete string abc without adding characters. A NULL text input returns NULL, while an empty string remains empty. I do not automatically convert these states into one shared label when a report needs to distinguish unknown data from a genuinely empty field.

Negative length has no universal interpretation. PostgreSQL documents removal of the first |n| characters: RIGHT('abc', -1) returns bc. SQL Server reports an error for a negative length. When the length comes from data, I require a positive integer for this task. I avoid calculating it unchecked from the position of a separator that might not exist.

Alternatives in Oracle and SQLite

In Oracle 19c I use SUBSTR with a negative starting position. The following expression selects the last three characters and returns XYZ. A negative position locates the beginning of the fragment by counting back from the end.

SELECT SUBSTR('ABC123XYZ', -3) AS suffix FROM DUAL;

SQLite also documents a negative position for SUBSTR. Its equivalent example omits DUAL: SELECT substr('ABC123XYZ', -3);. This is an alternative for the positive-n suffix task, not a claim that RIGHT and SUBSTR share every edge-case rule for their arguments.

Exercise: inspect the suffix assumption

Write the expected results for ABC123XYZ, UVW, A and NULL using a length of 3. Then decide whether a short code is allowed or should receive a source-error status. A function can correctly extract text from an invalid identifier. A useful analytical column needs both a format rule and an extraction rule.

Key takeaways

Can RIGHT extract any email domain?

A fixed character count produces a fixed-length suffix. Variable-length domains require a separator-based rule and separate input validation.

Does a negative length always cause an error?

No. PostgreSQL interprets it as omitting characters from the beginning, while SQL Server returns an error. Do not transfer that rule across engines.

How do Oracle and SQLite extract a suffix?

For positive n, use SUBSTR(text, -n). The Oracle 19c example uses FROM DUAL, while SQLite allows SELECT without a table.

AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.

Kajo Rudziński

Newsletter KajoData

Let’s keep in touch.

I write about data analysis, learning and changing careers. My newsletter and the sign-up form are in Polish. If that works for you, I’d be glad to see you there.