×

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 IF statement

The IF statement shows the true condition of the control flow function. The first condition is necessary to fulfill the requirement. The other condition is optional in the "IF" statement....

3 minutes read.

MySQL AVG function

This function works on numerical data type values. The average function shows the average value of the data set. If an average function returns a null value, then the row...

5 minutes read.

MySQL MIN function

The MIN() function determines the minimum or lowest value of the data set. This function works on numerical data type values. If the table displays zero value, then the row...

4 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 Character Length Function

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

2 minutes read.

MySQL Features

Features of MySQL MySQL is a relational database management system (RDBMS): It is a collection of many programs and makes relations with many other programs. Easy to use MySQL database: MySQL...

2 minutes read.

MySQL Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

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 RTRIM() function

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

2 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 Advance function

The advance function operates numerical values, string values, and data types.  The advance function converts, displays, and compares given values as per requirement. Here, you find out database, table, and...

4 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 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 Unique index

It helps to maintain data integrity to enforce the uniqueness of values in one or more columns. We can create more than one UNIQUE index in a single table, which...

3 minutes read.

MySQL LOG2() function

In this context, we will learn how we can use the MySQL LOG2() function to calculate the Logarithm of a specific number with base 2 with proper syntax and examples. Introduction...

2 minutes read.

MySQL vs Oracle

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL EQUI JOIN

An equijoin is an operation that combines multiple tables based on equality or matching column values in the associated tables. This operation links more than two tables based on a...

5 minutes read.

MySQL LENGTH() Function

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

2 minutes read.

MySQL CONCAT() function

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

3 minutes read.

MySQL DAY() function

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

2 minutes read.