How the RIGHT() function works in Mariadb?

The RIGHT() function in MariaDB is a string function that returns a given number of characters from the rightmost part of a string.

Posted on

The RIGHT() function in MariaDB is a string function that returns a given number of characters from the rightmost part of a string. It is a powerful tool that can be used for a variety of purposes, such as:

  • Extracting the last few characters of a string
  • Removing the first few characters of a string
  • Splitting a string into two parts
  • Converting text to a different format

This article will provide an overview of the RIGHT() function, including its syntax, arguments, and usage. We will also provide several examples to illustrate how the function can be used in different scenarios.

Syntax

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

RIGHT(str, len)

Where:

  • str is the string that you want to extract the rightmost characters from.
  • len is the number of characters that you want to return from the rightmost part of the string.

The RIGHT() function is case-sensitive, so it will only return characters from the rightmost part of the string that match the case of the string you are searching in.

Examples

The following are a few examples of how the RIGHT() function can be used:

Example 1: Extracting the last few characters of a string

Suppose you have a string that contains a product ID. You can use the RIGHT() function to extract the last few characters of the product ID as follows:

SELECT RIGHT('PRODUCT12345', 5);

This will return the following string:

45675

Example 2: Removing the first few characters of a string

Suppose you have a string that contains a file name. You can use the RIGHT() function to remove the first few characters of the file name as follows:

SELECT RIGHT('image.png', 5);

This will return the following string:

e.png

Example 3: Splitting a string into two parts

Suppose you have a string that contains a full name. You can use the RIGHT() function to split the string into two parts, the first name and the last name, as follows:

SELECT LEFT('John Doe', 4), RIGHT('John Doe', 4);

This will return the following two strings:

+---------------------+----------------------+
| LEFT('John Doe', 4) | RIGHT('John Doe', 4) |
+---------------------+----------------------+
| John                |  Doe                 |
+---------------------+----------------------+

Example 4: Converting text to a different format

Suppose you have a string that contains a phone number. You can use the RIGHT() function to convert the phone number to a different format as follows:

SELECT RIGHT('(123) 456-7890', 4);

This will return the following string:

+----------------------------+
| RIGHT('(123) 456-7890', 4) |
+----------------------------+
| 7890                       |
+----------------------------+

The following are some functions that are related to the RIGHT() function:

  • LEFT(): This function returns the leftmost characters from a string.
  • SUBSTR(): This function returns a substring from a string.
  • INSTR(): This function returns the position of the first occurrence of a substring within a string.
  • LTRIM(): This function removes leading spaces from a string.
  • RTRIM(): This function removes trailing spaces from a string.

Conclusion

The RIGHT() function is a powerful tool that can be used for a variety of purposes. By understanding the syntax and arguments of the function, you can use it to perform a variety of tasks on string data.