SQL 中的 TRIM 函数是用来移除掉一个字串中的字头或字尾.最常见的用途是移除字首或字尾的空白.
TRIM ([[位置] [要移除的字串] FROM ] 字串): [位置] 的可能值为 LEADING (起头),TRAILING (结尾), or BOTH (起头及结尾).这个函数将把 [要移除的字串] 从字串的起头、结尾,或是起头及结尾移除.如果我们没有列出 [要移除的字串] 是什么的话,那空白就会被移除.
TRIM(字串): 将所有字串起头或结尾的空白移除
LTRIM(字串): 将所有字串起头的空白移除.
RTRIM(字串): 将所有字串结尾的空白移除.
mysql> create table temp(
-> s varchar(32));
Query OK, 0 rows affected (0.06 sec)
-> s varchar(32));
Query OK, 0 rows affected (0.06 sec)
mysql> insert into temp values(
trim(' sample '));
Query OK, 1 row affected (0.02 sec)
Query OK, 1 row affected (0.02 sec)
mysql> insert into temp values(
ltrim(' sample '));
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.01 sec)
mysql> insert into temp values(
rtrim(' sample '));
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.01 sec)
mysql> select * from temp;
+----------+
| s |
+----------+
| sample |
| sample |
| sample |
+----------+
3 rows in set (0.00 sec)
+----------+
| s |
+----------+
| sample |
| sample |
| sample |
+----------+
3 rows in set (0.00 sec)
mysql> select length(s) from temp;
+-----------+
| length(s) |
+-----------+
| 6 |
| 8 |
| 8 |
+-----------+
3 rows in set (0.00 sec)
+-----------+
| length(s) |
+-----------+
| 6 |
| 8 |
| 8 |
+-----------+
3 rows in set (0.00 sec)
从这个实验看出,trim()的确移除了头部和尾部的空白,ltrim()只移除了头部的空白,rtrim()只移除了尾部的空白。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25281640/viewspace-710084/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/25281640/viewspace-710084/