欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql 强大的trim() 函数

程序员文章站 2024-02-29 10:11:58
mysql中的去除左空格函数: ltrim(str) returns the string str with leading space characters remove...
mysql中的去除左空格函数:
ltrim(str)
returns the string str with leading space characters removed.

以下是代码片段:

复制代码 代码如下:

mysql> select ltrim(' barbar');
-> 'barbar'


this function is multi-byte safe.

mysql中的去除右空格函数:
rtrim(str)
returns the string str with trailing space characters removed.

以下是代码片段:

复制代码 代码如下:

mysql> select rtrim('barbar ');
-> 'barbar'


this function is multi-byte safe.

trim函数可以过滤指定的字符串:
完整格式:trim([{both | leading | trailing} [remstr] from] str)
简化格式:trim([remstr from] str)

returns the string str with all remstr prefixes or suffixes removed. if none of the specifiers both, leading, or trailing is given, both is assumed. remstr is optional and, if not specified, spaces are removed.

以下是代码片段:

复制代码 代码如下:

mysql> select trim(' bar '); //默认删除前后空格
-> 'bar'
mysql> select trim(leading ',' from ',,barxxx'); //删除指定首字符 如',‘
-> 'barxxx'
mysql> select trim(both ',' from ',,bar,,,'); //删除指定首尾字符
-> 'bar'
mysql> select trim(trailing ',' from 'barxxyz,,');
-> 'barxxyz'


复制代码 代码如下:

mysql> update table set `field`=trim(trailing ',' from `field`) where where `field` like '%,';


this function is multi-byte safe.

替换数据库中字段的最后一个分页符

复制代码 代码如下:

update [!db.pre!]ecms_news_data_1 set `newstext`=trim(trailing '[!--empirenews.page--]' from `newstext`) where id=585;
select trim(trailing '[!--empirenews.page--]' from `newstext`) as newstex from [!db.pre!]ecms_news_data_1 where id=585;