×

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 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 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 Advance table Query

MySQL Advance table Query The table is created by index, rows, and columns in the MySQL database. The user saves their information in matrix format. The database requires a query to...

14 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 Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.

MySQL variable

The MySQL variable is essential for storing data in the table. The variable declares data with a specific name or label to avoid confusion. This data label is used in...

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

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 vs SQL

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

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 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 LOG10() function

In this context, we will learn how we can use the MySQL LOG10() function with proper syntax and good examples. Introduction of MySQL LOG10() function To evaluate the natural logarithmic value of...

2 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 Constraints

MySQL Constraints Introduction The constraints help to restrict what values should be stored in a table. The constraints provide limitations of the columns or data. This function helps to insert data in...

20 minutes read.

MySQL MAX function

The MAX function is a type of aggregation function that determines the maximum value of the table data. This function works on numerical data type values. If the table displays...

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

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 Invisible Index

Invisible Index MySQL index decides an option to the availability of the index for the query optimizer. This index shows the visible and invisible options to displays the index column in...

3 minutes read.