RIGHT()
Returns the last N characters from the right of a string.
Description
RIGHT returns the last n characters of a string, counting from the end. It's
the mirror of LEFT and handy for suffixes like file extensions or the last
digits of an identifier.
Syntax
RIGHT(string, n)Parameters
| Name | Description | Optional |
|---|---|---|
| string | The source text. | No |
| n | How many characters to take. | No |
Return Type
Returns a VARCHAR.
Examples
Employees & Departments
Loading database engine...
Employees & Departments
Loading database engine...
RIGHT vs SUBSTRING
RIGHT(s, n) is equivalent to SUBSTRING(s FROM LENGTH(s) - n + 1). Use
RIGHT for a clean suffix.
Common Mistakes
- n larger than the string.
RIGHTreturns the whole string; it doesn't error or pad. - Negative n. Behavior varies; prefer a non-negative count.
- Confusing with LEFT.
LEFTtakes from the start;RIGHTfrom the end.
Related Functions
See also: LEFT, SUBSTRING, LENGTH.