目录
语法
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)
参考