MariaDB RIGHT() Function

In MariaDB, the RIGHT() function extracts a given number of characters from the rightmost part of a string and returns it.

If you want to extract characters from left of a string, use the LEFT() function.

MariaDB RIGHT() Syntax

Here is the syntax of the MariaDB RIGHT() function:

RIGHT(str, len)

Parameters

str

Required. The string from which characters need to be extracted.

len

Required. The number of characters that need to be extracted from the string.

If you provide no parameters or use the wrong number of parameters, MariaDB will report an error: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1.

Return value

MariaDB RIGHT(str, len) extracts the specified number of characters from the rightmost part of the specified string and returns them as a string.

If len exceeds the length of str, the RIGHT() function returns str.

If len is zero or negative, the RIGHT() function returns an empty string.

If any of the arguments is NULL, the RIGHT() function will return NULL.

MariaDB RIGHT() Examples

To extract 2 characters from the rightmost part of the string ABCD, use the following statement:

SELECT RIGHT('ABCD', 2);

Output:

+------------------+
| RIGHT('ABCD', 2) |
+------------------+
| CD               |
+------------------+

The statement show usages of RIGHT().

SELECT
    RIGHT('Hello', 1),
    RIGHT('Hello', 2),
    RIGHT('Hello', 3),
    RIGHT('Hello', 0),
    RIGHT('Hello', -1),
    RIGHT('Hello', NULL),
    RIGHT(NULL, NULL)\G

Output:

*************************** 1\. row ***************************
   RIGHT('Hello', 1): o
   RIGHT('Hello', 2): lo
   RIGHT('Hello', 3): llo
   RIGHT('Hello', 0):
  RIGHT('Hello', -1):
RIGHT('Hello', NULL): NULL
   RIGHT(NULL, NULL): NULL

Conclusion

The MariaDB RIGHT() function extracts the specified number of characters from the leftmost part of a string.