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)

参考

相关推荐
小安运维日记30 分钟前
Linux云计算 |【第四阶段】NOSQL-DAY1
linux·运维·redis·sql·云计算·nosql
kejijianwen1 小时前
JdbcTemplate常用方法一览AG网页参数绑定与数据寻址实操
服务器·数据库·oracle
编程零零七2 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
Dingdangr3 小时前
Android中的Intent的作用
android
技术无疆3 小时前
快速开发与维护:探索 AndroidAnnotations
android·java·android studio·android-studio·androidx·代码注入
GEEKVIP3 小时前
Android 恢复挑战和解决方案:如何从 Android 设备恢复删除的文件
android·笔记·安全·macos·智能手机·电脑·笔记本电脑
高兴就好(石5 小时前
DB-GPT部署和试用
数据库·gpt
这孩子叫逆5 小时前
6. 什么是MySQL的事务?如何在Java中使用Connection接口管理事务?
数据库·mysql
Karoku0665 小时前
【网站架构部署与优化】web服务与http协议
linux·运维·服务器·数据库·http·架构
码农郁郁久居人下5 小时前
Redis的配置与优化
数据库·redis·缓存