×

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 logical conditions

MySQL logical conditions Introduction MySQL handles data with clauses, operators, and conditions. The logical condition is used to compare information and returns the required output. This condition applies logic to MySQL expressions...

7 minutes read.

MySQL LOG() function

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

3 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 INNER JOIN

MySQL inner join connects two tables by using their common columns. It is the basic join of the MySQL system. It is used to returns only those results from the...

4 minutes read.

MySQL Tutorial

In this tutorial, you will get information about the MySQL management system. This tutorial will cover the basic and advanced level MySQL concepts with examples that will help you become...

5 minutes read.

MySQL GREATEST function

This statement displays the greatest values of the table. The GREATEST function returns a null value when the table contains a null value. The GREATEST function needs a minimum of...

2 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 SEC_TO_TIME() function

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

2 minutes read.

MySQL Math Function

The math function operates numerical values and displays required number data. The math function finds out sin, cos, tan values. Here, you find out the absolute value, reminder, and logarithmic...

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 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 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.

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 DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement....

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

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

2 minutes read.

MySQL FROM_BASE64() function

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

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 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 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.