RIGHT()

SQL · String Functions

Theory

RIGHT(string, n) returns the last n characters of a string. RIGHT('9829012345', 4) returns '2345'. If n is longer than the string, the whole string comes back. It reads the string from the end, so it works even when values have different lengths, as long as the part you want has a fixed length at the end.

RIGHT is available in MySQL, SQL Server, Postgres and Snowflake but not in SQLite or Oracle. There, SUBSTR(string, -n) does the same job, because a negative start position counts from the end. A classic use is masking sensitive values: 'XXXXXX' || RIGHT(phone, 4) shows only the last four digits of a mobile number. Others are taking a fixed-length suffix such as a file extension or the last digits of an account number. Watch out for trailing spaces, especially in fixed-width CHAR columns: they count, so RTRIM the value first.

Why we use it

Many identifiers carry meaning at the end: last digits of a card or phone number, a check digit, a year suffix. RIGHT extracts them directly, which is perfect for masking data and for matching on suffixes.

When to use it

Use RIGHT when the part you need has a fixed length at the end of the string. In SQLite or Oracle use SUBSTR(str, -n). If the suffix length varies (like everything after the last dot), you need INSTR-style searching instead.

How it works

  1. Take the string and n
  2. n > string length? Yes: Return the whole string. No: Count n characters back from the end.
  3. Trailing spaces count as characters
  4. Return the suffix

Syntax

RIGHT(string, n)

Example

SELECT RIGHT(email, 10) FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic