In MariaDB, RIGHT()
is a built-in string function that returns a given number of characters from the rightmost part of a string.
RIGHT()
accepts two arguments; the string, and the number of characters to return from the right part of that string.
Syntax
The syntax goes like this:
RIGHT(str,len)
Where str
is the string, and len
is the number of characters to extract from the right part of the string.
Example
Here’s a basic example:
SELECT RIGHT('Aerospace', 5);
Result:
+-----------------------+ | RIGHT('Aerospace', 5) | +-----------------------+ | space | +-----------------------+
A Database Example
Here’s an example of getting the right part of the values in a database column:
SELECT
ProductName,
RIGHT(ProductName, 11) AS "Right part"
FROM Products;
Result:
+---------------------------------+-------------+ | ProductName | Right part | +---------------------------------+-------------+ | Left handed screwdriver | screwdriver | | Right handed screwdriver | screwdriver | | Long Weight (blue) | ight (blue) | | Long Weight (green) | ght (green) | | Sledge Hammer | edge Hammer | | Chainsaw | Chainsaw | | Straw Dog Box | raw Dog Box | | Bottomless Coffee Mugs (4 Pack) | gs (4 Pack) | +---------------------------------+-------------+
Null Arguments
If any (or all) of the arguments are null
, the RIGHT()
function returns null
:
SELECT
RIGHT(null, 3),
RIGHT('Coffee', null),
RIGHT(null, null);
Result:
+----------------+-----------------------+-------------------+ | RIGHT(null, 3) | RIGHT('Coffee', null) | RIGHT(null, null) | +----------------+-----------------------+-------------------+ | NULL | NULL | NULL | +----------------+-----------------------+-------------------+
Missing Arguments
Calling RIGHT()
without passing any arguments results in an error:
SELECT RIGHT();
Result:
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