SQL:TRIM()函数去除字符串头尾空格

目录

语法

sql 复制代码
TRIM ( [ LEADING | TRAILING | BOTH ] [characters FROM ] string )
Or
TRIM ( [ characters FROM ] string )
Or
TRIM ( string )

参数说明

  • BOTH - 它从字符串的起始和结束位置删除指定的字符(默认位置行为)。
  • LEADING - 它从字符串的起始位置删除指定的字符。
  • TRAILING - 它从字符串的结束位置删除指定的字符。

移除空格

sql 复制代码
-- 移除两侧空格
mysql> select trim(' hello ');
+-----------------+
| trim(' hello ') |
+-----------------+
| hello           |
+-----------------+
1 row in set (0.00 sec)

-- 移除开头空格
mysql> select ltrim(' hello ');
+------------------+
| ltrim(' hello ') |
+------------------+
| hello            |
+------------------+
1 row in set (0.01 sec)

-- 移除结尾空格
mysql> select rtrim(' hello ');
+------------------+
| rtrim(' hello ') |
+------------------+
|  hello           |
+------------------+
1 row in set (0.00 sec)

移除指定字符

sql 复制代码
-- 移除两侧指定字符
mysql> select trim('.' from '.hello.world.');
+--------------------------------+
| trim('.' from '.hello.world.') |
+--------------------------------+
| hello.world                    |
+--------------------------------+
1 row in set (0.00 sec)

-- 等价于
mysql> select trim(both '.' from '.hello.world.');
+-------------------------------------+
| trim(both '.' from '.hello.world.') |
+-------------------------------------+
| hello.world                         |
+-------------------------------------+
1 row in set (0.01 sec)


-- 移除开头指定字符
mysql> select trim(leading '.' from '.hello.world.');
+----------------------------------------+
| trim(leading '.' from '.hello.world.') |
+----------------------------------------+
| hello.world.                           |
+----------------------------------------+
1 row in set (0.01 sec)


-- 移除结尾指定字符
mysql> select trim(trailing '.' from '.hello.world.');
+-----------------------------------------+
| trim(trailing '.' from '.hello.world.') |
+-----------------------------------------+
| .hello.world                            |
+-----------------------------------------+
1 row in set (0.01 sec)

参考

相关推荐
AI Echoes17 分钟前
构建一个LangChain RAG应用
数据库·python·langchain·prompt·agent
峥嵘life32 分钟前
Android16 EDLA 认证测试CTS问题分析解决
android·java·服务器
@nengdoudou1 小时前
KingbaseES支持 mysql 的find_in_set函数
数据库·mysql
惟恋惜1 小时前
Jetpack Compose 的状态使用之“界面状态”
android·android jetpack
摇滚侠1 小时前
面试实战 问题三十三 Spring 事务常用注解
数据库·spring·面试
梁萌1 小时前
保姆级的MySQL执行计划(Explain)解读
数据库·mysql·explain·执行计划
JIngJaneIL1 小时前
基于Java+ vue智慧医药系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
+VX:Fegn08951 小时前
计算机毕业设计|基于springboot + vue图书管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
杨云龙UP2 小时前
MySQL 8.0.x InnoDB 写入链路优化:Redo Log 与 Buffer Pool 扩容与缓冲区调优实战记录-20251029
linux·运维·数据库·sql·mysql
黄俊懿3 小时前
【深入理解SpringCloud微服务】Seata(AT模式)源码解析——开启全局事务
java·数据库·spring·spring cloud·微服务·架构·架构师