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)

参考

相关推荐
NineData4 小时前
NineData 迁移评估功能正式上线
数据库·dba
火柴就是我9 小时前
让我们实现一个更好看的内部阴影按钮
android·flutter
NineData10 小时前
数据库迁移总踩坑?用 NineData 迁移评估,提前识别所有兼容性风险
数据库·程序员·云计算
阿里云大数据AI技术10 小时前
用 SQL 调大模型?Hologres + 百炼,让数据开发直接“对话”AI
sql·llm
赵渝强老师12 小时前
【赵渝强老师】PostgreSQL中表的碎片
数据库·postgresql
砖厂小工16 小时前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
全栈老石16 小时前
拆解低代码引擎核心:元数据驱动的"万能表"架构
数据库·低代码
张拭心16 小时前
春节后,有些公司明确要求 AI 经验了
android·前端·人工智能
张拭心17 小时前
Android 17 来了!新特性介绍与适配建议
android·前端
Kapaseker19 小时前
Compose 进阶—巧用 GraphicsLayer
android·kotlin