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)

参考

相关推荐
马克Markorg2 小时前
常见的向量数据库和具有向量数据库能力的数据库
数据库
百锦再3 小时前
React编程高级主题:测试代码
android·前端·javascript·react.js·前端框架·reactjs
2501_916008894 小时前
全面介绍Fiddler、Wireshark、HttpWatch、SmartSniff和firebug抓包工具功能与使用
android·ios·小程序·https·uni-app·iphone·webview
Coder_Boy_5 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
玉梅小洋5 小时前
Windows 10 Android 构建配置指南
android·windows
helloworldandy5 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
Libraeking6 小时前
视觉篇:Canvas 自定义绘图与高级动画的华丽圆舞曲
android·经验分享·android jetpack
数据知道7 小时前
PostgreSQL 故障排查:如何找出数据库中最耗时的 SQL 语句
数据库·sql·postgresql
qq_12498707537 小时前
基于SSM的动物保护系统的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·毕业设计·ssm·计算机毕业设计
枷锁—sha7 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全