MySQL清除字符串首尾空格函数

数据库 MySQL
下文为您介绍的几个MySQL函数都属于MySQL清除字符串首尾空格函数,如果您对MySQL函数方面感兴趣的话,不妨一看。

MySQL清除字符串首尾空格函数是经常会用到的函数,MySQL 清除字符串首尾空格函数有三个: ltrim(), rtrim(), trim(),下面就为您分别介绍。

  1. mysql> select concat('.', ltrim(' ddd '), '.');  
  2. +----------------------------------+  
  3. | concat('.', ltrim(' ddd '), '.') |  
  4. +----------------------------------+  
  5. | .ddd .                           |  
  6. +----------------------------------+  
  7. mysql> select concat('.', rtrim(' ddd '), '.');  
  8. +----------------------------------+  
  9. | concat('.', rtrim(' ddd '), '.') |  
  10. +----------------------------------+  
  11. | . ddd.                           |  
  12. +----------------------------------+  
  13. mysql> select concat('.', trim(' ddd '), '.');  
  14. +---------------------------------+  
  15. | concat('.', trim(' ddd '), '.') |  
  16. +---------------------------------+  
  17. | .ddd.                           |  
  18. +---------------------------------+ 

MySQL 中的 trim 字符串函数,实在是强大。它不仅能消除字符串首尾部的空格,还可以消除我们指定的任意字符。ltrim(), rtrim() 只是它的一个功能子集。来看下 trim 函数的完整语法:

1. trim([{both | leading | trailing} [remstr] from] str)
2. trim([remstr from] str)

1. 清除字符串首部字符。

  1. ysql> select trim(leading '.' from '..ddd..');  
  2. +----------------------------------+  
  3. | trim(leading '.' from '..ddd..') |  
  4. +----------------------------------+  
  5. | ddd..                            |  
  6. +----------------------------------+ 

2. 清除字符串尾部字符。

  1. mysql> select trim(trailing '.' from '..ddd..');  
  2. +-----------------------------------+  
  3. | trim(trailing '.' from '..ddd..') |  
  4. +-----------------------------------+  
  5. | ..ddd                             |  
  6. +-----------------------------------+ 

3. 清除字符串首尾部字符。

  1. mysql> select trim(both '.' from '..ddd..');  
  2. +-------------------------------+  
  3. | trim(both '.' from '..ddd..') |  
  4. +-------------------------------+  
  5. | ddd                           |  
  6. +-------------------------------+  
  7. mysql> select trim('.' from '..ddd..');  
  8. +--------------------------+  
  9. | trim('.' from '..ddd..') |  
  10. +--------------------------+  
  11. | ddd                      |  
  12. +--------------------------+ 

trim() 默认清除字符串首尾部的空格。

以上就是MySQL清除字符串首尾空格函数的介绍。

 

 

【编辑推荐】

MySQL字符串大小写转换函数

MySQL concat函数的使用

获得自动增长的MySQL主键

MySQL主键的设计原则

MySQL分区的功能和限制

责任编辑:段燃 来源: 互联网
相关推荐

2010-11-26 11:20:31

MySQL字符串处理函

2010-05-26 15:24:09

MySQL字符串

2010-10-09 11:54:46

MySQL字符串

2010-11-26 10:14:40

MySQL repla

2010-11-26 11:34:32

MySQL截取字符串函

2009-02-24 15:39:27

字符串比较函数函数

2023-03-06 23:05:32

MySQL字符串函数

2010-05-26 15:36:23

MySQL字符串

2010-11-26 09:46:26

MySQL字符串相加

2009-02-24 14:27:55

2010-05-26 15:14:39

MySQL字符串

2014-01-02 16:14:10

PostgreSQL字符串

2010-10-11 15:47:46

MySQL字符串大小写

2010-11-26 13:47:51

MySQL随机字符串

2010-10-09 12:01:00

MySQL字符串

2010-11-26 10:22:28

MySQL字符串比较

2010-11-26 09:51:54

MySQL字符串

2023-01-11 16:49:13

MySQL数据库

2019-09-18 09:56:41

MySQLSQL函数

2010-09-09 11:48:00

SQL函数字符串
点赞
收藏

51CTO技术栈公众号