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
SQRT() Function in MySQL
The SQRT() function in MySQL calculates the square root of a non-negative number, returning NULL for negative inputs. It is a built-in function that provides high precision and is optimized for performance and making it ideal for mathematical and scientific applications.In the article, we will cover
3 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
RTRIM() Function in MySQL
RTRIM() : It is the function in MySQL that is used to remove trailing spaces from a string. Syntax : RTRIM(str) Parameter : RTRIM() function accepts one parameter as mentioned above and described below. str âThe string from which we want to remove trailing spaces. Returns : It returns a string after
3 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
STD() function in MySQL
With the help of STD() function we can calculate population Standard deviation of an expression in MySQL. But, if there are no matching rows in the given expression it returns Null. Syntax : STD(expr); Parameter : This method accepts only one parameter. expr : Input expression from which we want to
3 min read
TIME() Function in MySQL
The TIME() function in MySQL is used to extract the time portion from a date or datetime expression, returning the time in the format 'HH:MM'. This function is particularly useful when working with time components in databases, such as scheduling or logging systems. In this article, We will learn ab
4 min read