×

MySQL RIGHT() Function

In this context, we will learn how we can use the MySQL RIGHT() Function with proper syntax and good examples.

Introduction of MySQL RIGHT() function

The RIGHT() function in MySQL is used to extract a specified number of characters from the right side of a given string. The second argument is used to decide how many characters it should return.

Syntax of the MySQL RIGHT() Function

The syntax of the MySQL RIGHT() function is given as follows:

RIGHT(str, len)

Parameters or arguments used in MySQL RIGHT() Function:

There are two parameters accepted by the RIGHT() Function in MySQL, which are given as follows:

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 the string, this Function will return the actual string.

Returns:

It will return a number of characters from a string (starting from the right).

Application used for RIGHT() function:

The RIGHT() function can be used in the given below MySQL versions.:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

Examples of MySQL RIGHT() function:

Now we will look into some MySQL RIGHT() function examples and will explore how we can use the RIGHT function in MySQL.

Example-1 :

In this example, we will Apply the RIGHT() Function to a given string in MySQL.

SELECT RIGHT("JAVATPOINT", 4) AS Right_Str;

Output:

Right_Str
OINT
1 row in set (0.00 sec)

Example 2:

In this example, we will Apply the RIGHT() Function to a given number in MySQL.

SELECT RIGHT(12345678, 4) AS Right_Num;

Output:

Right_Num
5678
1 row in set (0.00 sec)

Example-3:

In this example, we will Apply the RIGHT() Function to a given string in MySQL

when len > characters in the string.

SELECT RIGHT("JAVATPOINT", 20) AS Right_Str;

Output:

Right_Str
JAVATPOINT
1 row in set (0.00 sec)

Example-4 :

When we require to find the Last Name of the Cricketer in a table, then we can take the help of the RIGHT function in MySQL. To demonstrate, create a table named Cricketer.

CREATE TABLE Cricketer(
Cricketer_id INT AUTO_INCREMENT,
Cricketer_name VARCHAR(100) NOT NULL,
Playing_team VARCHAR(20) NOT NULL,
PRIMARY KEY(Cricketer_id )
);

Now, we will insert some data into the Cricketer table :

INSERT INTO
Cricketer(Cricketer_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 Cricketer Table is :

mysql> SELECT * FROM Cricketer;
Cricketer_idCricketer_namePlaying_team
1Virat KohliRCB
2Rohit SharmaMI
3Dinesh KarthikKKR
4Shreyash IyerDC
5David WarnerSRH
6Steve SmithRR
7Andre RussellKKR
8Jasprit BumrahMI
9Risabh PanthDC

Now we will find the Last Name of every Cricketer, to find the last name, we have to apply the following approach-

First, use INSTR() Function to find the location of the space ( ) in the Name.

Secondly, we will use the LENGTH() Function to find the length of the Cricketer's Name. Here 'len' of the RIGHT Function will be the length of Cricketer_name minus the location of the ''(space) character.

Third, use the RIGHT( ) Function to extract the Last Name of the Cricketer.

SELECT
Cricketer_name,
RIGHT(Cricketer_name, LENGTH(Cricketer_name) - INSTR(Cricketer_name, ' ')) Lastname,
Playing_team
FROM
Cricketer;

Output:

Cricketer_nameLastnamePlaying_team
Virat KohliKohliRCB
Rohit SharmaSharmaMI
Dinesh KarthikKarthikKKR
Shreyash IyerIyerDC
David WarnerWarnerSRH
Steve SmithSmithRR
Andre RussellRussellKKR
Jasprit BumrahBumrahMI
Risabh PanthPanthDC

Application of MySQL RIGHT() function:

This Function is used to extract a specified number of characters from the right side of a given string.

Summary:

In the above context, we have learned how we can use the RIGHT() Function in MySQL used to extract a specified number of characters from the right side of a given string.


Related Topics

MySQL SELF JOIN

This join links table with itself. The inner join, a self join, a right join, and a cross join are connected with two or more two tables. But, the "self...

4 minutes read.

MySQL FIND_IN_SET() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function MySQL provides a built-in string function called...

4 minutes read.

MySQL Prefix index

Prefix index This index query creates an index column in the string or character column. MySQL system can create multiple indexes in the table. It will create in a table as...

3 minutes read.

MySQL Truncate() Function

In this context, we will learn how we can use the MYSQL TRUNCATE function to truncate a number to a mentioned number of decimal places. Syntax of MySQL Truncate Function A number...

2 minutes read.

MySQL TAN() Function

Basically, the TAN() function in MySQL is used to give back the tangent of a mentioned number.In any kind of right-angle triangle, the tangent of an angle is the length...

2 minutes read.

MySQL FLOOR() function

In this context, we will learn how we can use the MySQL FLOOR() function with proper syntaxes and examples. Introduction of MySQL Floor() function: FLOOR() function in MySQL is used to return...

3 minutes read.

Clustered vs Non-Clustered Index

Clustered vs. Non-Clustered Index The difference between clustered and non-clustered index is the most famous question in the database-related interviews. Both indexes have the same physical structure and are stored as...

2 minutes read.

MySQL CEILING() function

In this context, we will learn how we can use the MySQL CEILING () function with proper syntax and good examples. Introduction of MySQL CEILING () function The CEILING() function accepts the...

2 minutes read.

MySQL Triggers

MySQL trigger is a function of the stored procedure to respond to the system program. This function responds and runs any data table event automatically. You can use it for...

5 minutes read.

MySQL SOUNDEX() function

In this context, we will learn how we can use the MySQL SOUNDEX() function with proper syntax and good examples. Introduction of MySQL SOUNDEX() function SOUNDEX() function in MySQL is used to...

3 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL Table

Introduction MySQL table is an essential part of the system. MySQL table stores data using index, rows, and columns. It accesses data from the server quickly because of the table—this table...

22 minutes read.

MySQL SUM function

The SUM() function displays the total addition of the table values. It supports the arithmetic operation of the column values. This function does not return a null value. Here, the...

5 minutes read.

MySQL Union

MySQL contains the same categories of data in a different table. Sometimes, you require multiple tables to collect data together. MySQL union is a function to combine two table's data...

5 minutes read.

MySQL LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 minutes read.

MySQL Data Types

Introduction of the Data types In the data file, we have several information such as names, numbers, marks, and other information. This information specifies categories such as numbers, characters, and decimal...

8 minutes read.

MySQL Descending Index

Descending Index It is a type of index that stores key values in descending order. This query improves the performance of an index query. MySQL system displays the index as per...

3 minutes read.

MySQL LOCATE() function

In this context, we will learn how we can use the MySQL LOCATE() function with proper syntax and good examples. Introduction of MySQL LOCATE() function LOCATE() function in MySQL is used for...

3 minutes read.

MySQL Control flow function

The control flow function uses values or operands for logical operations. These functions can works on single and multiple conditions. The control flow function is based on the Boolean expression....

3 minutes read.

MySQL View

Introduction MySQL View is a virtual table to create a clone of the base table. The View does not contain its values or data. MySQL View creates to connect more than...

12 minutes read.