Different ways to use RIGHT() Function in MySQL8.

RIGHT() function fetches a number of characters from a string, starting from right. How many characters will be fetched ? It is determined by second parameter i.e length.

It will return NULL, if any of the argument is NULL.

If length parameter has more than the number of characters in string, then string will be returned.

Note : This function is multibyte safe.

RIGHT() : Syntax

RIGHT ( string, length );

RIGHT() : Parameter

[table caption=”” width=”100%” colwidth=”15%|30%|55%” colalign=”left|left|left”]
Name, Required /Optional, Description
string, Required, it is a valid input string.
length, Required, it specifies the length of characters to be fetched.
[/table]


RIGHT() : Output

[table caption=”” width=”100%” colwidth=”20%|80%” colalign=”left|left”]
Return, Description
String, returns the leftmost length characters from the string.
NULL, if any argument is NULL.
[/table]


RIGHT() : Available from

[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, MySQL 4.0
[/table]


RIGHT() Example 1 :  NULL Arguments

It will return NULL, if any of the argument is NULL. Lets see the below example when length, second argument is NULL.

mysql> SELECT RIGHT('tutorialmines',NULL);
+-----------------------------+
| RIGHT('tutorialmines',NULL) |
+-----------------------------+
| NULL                        |
+-----------------------------+
1 row in set (0.03 sec)

Lets see the below example, when string, first argument is NULL.

mysql> SELECT RIGHT(NULL,5);
+---------------+
| RIGHT(NULL,5) |
+---------------+
| NULL          |
+---------------+
1 row in set (0.05 sec)


RIGHT() Example 2 : Basic Usage

Lets see the below example. I will select 8 characters from the right of string.

mysql> SELECT RIGHT('tutorialmines',8);
+--------------------------+
| RIGHT('tutorialmines',8) |
+--------------------------+
| ialmines                 |
+--------------------------+
1 row in set (0.00 sec)

RIGHT() Example 3 :

If length parameter has more than the number of characters in string.

Lets see the below example. Full string is returned in it.

mysql> SELECT RIGHT('tutorialmines',28);
+---------------------------+
| RIGHT('tutorialmines',28) |
+---------------------------+
| tutorialmines             |
+---------------------------+
1 row in set (0.00 sec))

RIGHT()  Example 4 : Database Query

Lets see the below example of how it works within a database query:

mysql> Select  Name as original, RIGHT(Name,3) as Changed 
FROM tbl_employee LIMIT 5;
+------------+---------+
| original   | Changed |
+------------+---------+
| Jyoti Rani | ani     |
| Polla      | lla     |
| Arnav      | nav     |
| Jeevesh    | esh     |
| Tom        | Tom     |
+------------+---------+
5 rows in set (0.01 sec)

See all MySQL String functions MySQL 8 String Functions.


Related articles : UCASE(), UPPER(), LCASE(), LOWER(), TRIM(), RTRIM(), LEFT().


PHP Related articles : TRIM(), LTRIM(), RTRIM(), STRSTR() ,STRCSPN() , STRCHR(), SUBSTR(), PHP STRING FUNCTIONS().


You may also like...