3

我想从 MySQL 中的字符串中删除前导和尾随空格( SP、\n、\t、最终 \r )。数据已经在 MySQL 表上,我无法检索它们以在 PHP 中进行处理,因为这应该太慢了。

我尝试了这种语法:

UPDATE table set field = TRIM(BOTH '\t' FROM TRIM(BOTH '\n' FROM TRIM(field)));

但是,这种方式删除了spaces, then\n , then\t in this order, and I want to remove all spaces disregarding their order (ie:"\n\t \t\n\n\t hello\t\n\n \t " would return only"hello"`。

我想我需要创建一个函数(CREATE FUNCTION MY_TRIM...),但在做这样的工作之前,我想知道是否有更简单的方法。

4

3 回答 3

0

你可以试试这个:

    UPDATE table SET `field`=TRIM(BOTH '\t' OR '\n' FROM TRIM(BOTH '\n' OR '\t' FROM TRIM(field))); 
于 2013-09-11T10:01:47.023 回答
0

为什么不简单地使用替换

例如

update table set field = replace(replace(replace(field,"\t",""),"\n","")," ","")
于 2013-09-11T09:32:38.980 回答
0

This Worked for me: REPLACE(REPLACE(FileName, '\r', ''), '\n', '')

于 2021-12-24T04:42:36.077 回答