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)

参考

相关推荐
先吃饱再说3 小时前
存储的进化:从 MySQL 到浏览器缓存,数据到底住在哪?
数据库
Nturmoils4 小时前
字段太多看不全,ksql 的展开模式和输出控制怎么用
数据库·后端
CYY954 小时前
Compose 入门篇
android·kotlin
Databend6 小时前
Agent 轨迹分析与归因的数据工程实践
大数据·数据库·agent
这个DBA有点耶6 小时前
SQL改写进阶:标量子查询的“隐形代价”与消除实战
数据库·mysql·架构
杉氧7 小时前
Compose 时代的 MVI 架构:如何用单向数据流驱动复杂 UI?
android·架构·android jetpack
杉氧7 小时前
Modifier 的艺术:为什么链式调用的顺序决定了UI 的生命周期?
android·架构·android jetpack
smallyoung7 小时前
数据库乐观锁深度解析:MySQL、PostgreSQL 实战 + Spring Boot 集成指南
数据库·mysql·postgresql
parade岁月7 小时前
MySQL JOIN解析:朴实无华但食之有味
数据库·后端
李斯维8 小时前
腾讯 XLog 日志框架 Android 端接入
android·android studio·android jetpack