MySQL RIGHT | Extract Characters from Right of String

MySQL Right is an inbuilt function in MySQL. It extracts a number of characters from a string starting from the right. In this article, we will discuss the MySQL Right Function. Also, we will discuss a few examples of using it.

Also, you can read about MySQL LEFT Function.

Syntax

RIGHT(stringnumberOfCharacters)

Parameters

The function expects two parameters. Also, both parameters are mandatory. The description of the parameters is as follows:

  • string: The first parameter to the function is the string to operate on.
  • numberOfCharacters: The second parameter to the function is the number of parameters to extract from the right side.

Return Value

The function returns a string consisting of characters extracted from the right side of the string.

MySQL Right Function

Examples

Let’s discuss a few examples of using the function

Example 1: Simple Extraction

mysql> SELECT RIGHT('BRINGING KNOWLEDGE TOGETHER', 8) as extracted;
+-----------+
| extracted |
+-----------+
| TOGETHER  |
+-----------+

In the above example, the function extracts 8 characters from the right side of the string.

Example 2: Negative or 0 Offset

However, if the number of characters for extraction are zero or negative, the function returns an empty string.

 SELECT RIGHT('BRINGING KNOWLEDGE TOGETHER', -1) as extracted;
+-----------+
| extracted |
+-----------+
|           |
+-----------+

 SELECT RIGHT('BRINGING KNOWLEDGE TOGETHER', 0) as extracted;
+-----------+
| extracted |
+-----------+
|           |
+-----------+

Example 3: Number of Characters Greater than Length of String

Also, the MySQL RIGHT Function returns the whole string if the number of characters is greater than the length of the string.

 SELECT RIGHT('BRINGING KNOWLEDGE TOGETHER', 500) as extracted;
+-----------------------------+
| extracted                   |
+-----------------------------+
| BRINGING KNOWLEDGE TOGETHER |
+-----------------------------+

Conclusion

In this article, we discussed the MySQL RIGHT Function. You can read more about it on MySQL Official Documentation. Additionally, you can read more about MySQL String Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *