RIGHT() Function in MySQL
Last Updated :
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 |
+----------------+----------+--------------+
Similar Reads
SIGN() Function in MySQL
SIGN() function in MySQL is used to return the sign of the given number. It returns 1 if the number is positive, -1 if the number is negative and 0 for zero. Syntax : SIGN(X) Parameter : SIGN() function accepts one parameter as input and will give you the results in values like Positive(+1),Negative
1 min read
TRIM() Function in MySQL
TRIM() function in MySQL is used to clean up data. It is also used to remove the unwanted leading and trailing characters in a string. Syntax : TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str) Parameter : This method accepts three-parameter as mentioned above and described below : BOTH | LEADIN
2 min read
PI() function in MySQL
PI() function in MySQL is used to return the Pi value. The default number of decimal places displayed is seven, but MySQL uses the full double-precision value internally. Syntax : PI() Parameter : This method does not accept any parameter. Returns : It returns the Pi value i.e. 3.141593. Example-1 :
2 min read
TAN() Function in MySQL
TAN() function : This function in MySQL is used to return the tangent of a specified number. In any right triangle, the tangent of an angle is the length of the opposite side divided by the length of the adjacent side. Similarly, this can also be defined as tangent of x is the sine of x divided by t
1 min read
ORD() Function in MySQL
ORD() function in MySQL is used to find the code of the leftmost character in a string . If the leftmost character is not a multibyte character, it returns ASCII value. And if the leftmost character of the string str is a multibyte character, ORD returns the code for that character, calculated from
3 min read
OCT() function in MySQL
OCT() function in MySQL is used to convert decimal number to octal. It returns equivalent octal value of a decimal number. Syntax : OCT(number) Parameter : This method accepts only one parameter. number : The decimal number which we want to convert. Returns : It returns octal value of a decimal numb
2 min read
REPEAT() function in MySQL
REPEAT() : This function in MySQL is used to repeat a string a specified number of times. Syntax : REPEAT(str, count) Parameters : This method accepts two parameter. str -Input String which we want to repeat. count -It will describe that how many times to repeat the string. Returns : It returns a re
2 min read
SUBSTRING() function in MySQL
SUBSTRING() : function in MySQL is used to derive substring from any given string .It extracts a string with a specified length, starting from a given location in an input string. The purpose of substring is to return a specific portion of the string. Syntax : SUBSTRING(string, start, length) OR SUB
2 min read
RPAD() Function in MySQL
RPAD() function in MySQL is used to pad or add a string to the right side of the original string. Syntax : RPAD(str, len, padstr) Parameter : This function accepts three parameter as mentioned above and described below : str : The actual string which is to be padded. If the length of the original st
1 min read
YEAR() Function in MySQL
YEAR() function in MySQL is used to find year from the given date. If the date is NULL, the YEAR() function will return NULL. Otherwise, it returns value range from 1000 to 9999. Syntax : YEAR(date) Parameter : This method accepts one parameter as mentioned above and described below : date : The dat
3 min read