MySQL RIGHT() Function
In MySQL, the RIGHT()
function returns the rightmost N
characters of a string.
If you want to get the leftmost N
characters of a string, use the LEFT()
function.
RIGHT()
Syntax
Here is the syntax of MySQL RIGHT()
function:
RIGHT(string, length)
Parameters
string
- Required. The string from which characters need to be extracted.
length
- Required. The number of characters that need to be extracted from the string.
Return value
RIGHT(string, length)
returns the rightmost length
characters of a string.
- If
length
exceeds the length ofstring
, the function returns thestring
. - If
length
is zero or negative, the function returns an empty string. - The function will return
NULL
if either parameter isNULL
.
RIGHT()
Examples
Here are some examples of MySQL RIGHT()
function.
SELECT
RIGHT('Hello', 1),
RIGHT('Hello', 2),
RIGHT('Hello', 3),
RIGHT('Hello', 0),
RIGHT('Hello', -1),
RIGHT('Hello', NULL),
RIGHT(NULL, NULL)\G
RIGHT('Hello', 1): H
RIGHT('Hello', 2): He
RIGHT('Hello', 3): Hel
RIGHT('Hello', 0):
RIGHT('Hello', -1):
RIGHT('Hello', NULL): NULL
RIGHT(NULL, NULL): NULL