Home > Database > Mysql Tutorial > How to use the RIGHT function in MySQL to intercept the right part of a string

How to use the RIGHT function in MySQL to intercept the right part of a string

王林
Release: 2023-07-12 10:20:02
Original
1181 people have browsed it

How to use the RIGHT function in MySQL to intercept the right part of a string

In MySQL, the RIGHT function is a function used to intercept the right part of a string. It accepts two parameters: the string to be intercepted and the length to be intercepted, and returns a string containing the specified length.

Using the RIGHT function can easily obtain the right part of the string. Below we will demonstrate how to use the RIGHT function through code examples.

First, we need to create a sample data table to store the string to be intercepted.

CREATE TABLE `strings` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `text` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
);

INSERT INTO `strings` (`text`) VALUES
  ('Hello'),
  ('World'),
  ('MySQL is a relational database management system.');
Copy after login

Next, we can use the RIGHT function to intercept the right part of the string.

SELECT RIGHT(`text`, 3) AS `right_text` FROM `strings`;
Copy after login

Run the above code, we will get the following results:

+------------+
| right_text |
+------------+
| llo        |
| rld        |
| tem.       |
+------------+
Copy after login

By using the RIGHT function, we successfully intercepted the right part of the string, with a length of 3.

It should be noted that the first parameter of the RIGHT function is the string to be intercepted, and the second parameter is the length to be intercepted. If the length to be truncated exceeds the length of the string, the entire string will be returned.

In addition to specifying a fixed length, we can also use variables or other functions to determine the length to be intercepted. For example, we can use the LENGTH function to get the length of a string, and then specify the intercepted length through a variable.

SET @length := 5; -- 设置截取的长度
SELECT RIGHT(`text`, @length) AS `right_text` FROM `strings`;
Copy after login

Through the above code, we can dynamically set the intercepted length.

To summarize, the RIGHT function in MySQL is a very useful function that can help us intercept the right part of the string. It is very simple and easy to use, you only need to specify the string to be intercepted and the length to be intercepted. Whether it is fixed length or dynamic length, the RIGHT function can meet our needs. I hope this article will help you master the use of the RIGHT function.

The above is the detailed content of How to use the RIGHT function in MySQL to intercept the right part of a string. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template