Home > Database > Mysql Tutorial > body text

How to remove both leading and trailing spaces from a string using the MySQL LTRIM() and RTRIM() functions?

王林
Release: 2023-08-27 15:25:02
forward
1088 people have browsed it

如何使用 MySQL LTRIM() 和 RTRIM() 函数同时删除字符串中的前导空格和尾随空格?

For removing both leading and trailing spaces at once from a string by using LTRIM() and RTRIM() functions, we must have to use one function as an argument for other function. In other words, we must have to pass either LTRIM() function as an argument of RTIM() function or vice versa. It can be understood from the following example −

Example

Suppose we have a table 'test_trim' having a column 'Name' containing the values ​​with leading and trailing spaces both −

mysql> Select * from test_trim;
+---------------+
| Name          |
+---------------+
| Gaurav        |
| Rahul         |
| Aarav         |
+---------------+
3 rows in set (0.00 sec)
Copy after login

Now, the following query will remove the leading and trailing spaces from the name in one go using LTRIM() and RTRIM() functions Trailing spaces−

mysql> Select Name, LTRIM(RTRIM(Name))AS 'Name Without Spaces' from test_trim;
+---------------+---------------------+
| Name          | Name Without Spaces |
+---------------+---------------------+
| Gaurav        | Gaurav              |
| Rahul         | Rahul               |
| Aarav         | Aarav               |
+---------------+---------------------+
3 rows in set (0.00 sec)

mysql> Select Name, RTRIM(LTRIM(Name))AS 'Name Without Spaces' from test_trim;
+---------------+---------------------+
| Name          | Name Without Spaces |
+---------------+---------------------+
| Gaurav        | Gaurav              |
| Rahul         | Rahul               |
| Aarav         | Aarav               |
+---------------+---------------------+
3 rows in set (0.00 sec)
Copy after login

The above is the detailed content of How to remove both leading and trailing spaces from a string using the MySQL LTRIM() and RTRIM() functions?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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