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

2023年 8月 27日 37.4k 0

如何使用 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)

登录后复制

现在,以下查询将使用LTRIM()和RTRIM()函数一次性从名称中删除前导和尾随空格 −

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)

登录后复制

以上就是如何使用 MySQL LTRIM() 和 RTRIM() 函数同时删除字符串中的前导空格和尾随空格?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论