RIGHT() Function in MySQL

30 Sep, 2020
RIGHT() function in MySQL is used to extract a specified number of characters from the right side of a given string. Second argument is used to decide, how many characters it should return.
Syntax :
RIGHT( str, len )

Parameter : This function accepts two parameter as mentioned above and described below :
str : The given string from whose right side a number of characters are to be extracted.
len : The number of characters to extract. If this parameter is larger than the number of characters in string, this function will return the actual string.
Returns : It returns a number of characters from a string (starting from right).
Example-1 : Applying RIGHT() Function to a given string.
SELECT RIGHT("geeksforgeeks", 4) AS Right_Str;

Output :
Example-2 : Applying RIGHT() Function to a number.
SELECT RIGHT(12345678, 4) AS Right_Num;

Output :
Example-3 : Applying RIGHT() Function to a given string when len > characters in string.
SELECT RIGHT("geeksforgeeks", 20) AS Right_Str;

Output :
Example-4 : Applying RIGHT() Function to find last name of Player in a table. To demonstrate create a table named Player.
CREATE TABLE Player(

    Player_id INT AUTO_INCREMENT,  
    Player_name VARCHAR(100) NOT NULL,
    Playing_team VARCHAR(20) NOT NULL,
    PRIMARY KEY(Player_id )

);
Now, inserting some data into Player table :
INSERT INTO  
    Player(Player_name, Playing_team)

VALUES
    ('Virat Kohli', 'RCB' ),
    ('Rohit Sharma', 'MI' ),
    ('Dinesh Karthik', 'KKR'  ),
    ('Shreyash Iyer', 'DC'  ),
    ('David Warner', 'SRH' ),
    ('Steve Smith', 'RR'  ),
    ('Andre Russell', 'KKR' ),
    ('Jasprit Bumrah', 'MI' ),
    ('Risabh Panth', 'DC'  ) ;

So, the Player Table is :
mysql> SELECT * FROM Player;
+-----------+----------------+--------------+
| Player_id | Player_name    | Playing_team |
+-----------+----------------+--------------+
|         1 | Virat Kohli    | RCB          |
|         2 | Rohit Sharma   | MI           |
|         3 | Dinesh Karthik | KKR          |
|         4 | Shreyash Iyer  | DC           |
|         5 | David Warner   | SRH          |
|         6 | Steve Smith    | RR           |
|         7 | Andre Russell  | KKR          |
|         8 | Jasprit Bumrah | MI           |
|         9 | Risabh Panth   | DC           |
+-----------+----------------+--------------+

Now we will find the Last name of every player, to find the last name we have to apply following approach-
First, using INSTR() function to find the location of the space ( ) in the Name.
Second, using the LENGTH() function to find the length of the player name. Here ‘len’ of RIGHT Function will be the length of Player_name minus the location of the ‘ ‘ (space) character.
Third, using the RIGHT( ) function to extract the Last name of Player.
SELECT 
    Player_name,
    RIGHT(Player_name, LENGTH(Player_name) - INSTR(Player_name, ' ')) Lastname,
    Playing_team
FROM
    Player;

Output :
+----------------+----------+--------------+
| Player_name    | Lastname | Playing_team |
+----------------+----------+--------------+
| Virat Kohli    | Kohli    | RCB          |
| Rohit Sharma   | Sharma   | MI           |
| Dinesh Karthik | Karthik  | KKR          |
| Shreyash Iyer  | Iyer     | DC           |
| David Warner   | Warner   | SRH          |
| Steve Smith    | Smith    | RR           |
| Andre Russell  | Russell  | KKR          |
| Jasprit Bumrah | Bumrah   | MI           |
| Risabh Panth   | Panth    | DC           |
+----------------+----------+--------------+

DBMS-SQL
mysql
SQL
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.