Is there a way to trim several characters (disregarding their combination) in MySQL?

半腔热情 提交于 2020-01-14 14:35:09

问题


I want to remove leading and trailing spaces ( SP, \n, \t, eventually \r ) from strings in MySQL. Data already are on MySQL tables and I can't retrieve them for processing in PHP as this should be too slow.

I tried this kind of syntax:

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

But, this way removes spaces, then\n, then\tin 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"`.

I guess I need to create a function (CREATE FUNCTION MY_TRIM...), but before doing such job, I would like to know if there are easier ways.


回答1:


why not simply use replace

eg

update table set field = replace(replace(replace(field,"\t",""),"\n","")," ","")



回答2:


You can try this:

    UPDATE table SET `field`=TRIM(BOTH '\t' OR '\n' FROM TRIM(BOTH '\n' OR '\t' FROM TRIM(field))); 


来源:https://stackoverflow.com/questions/18737374/is-there-a-way-to-trim-several-characters-disregarding-their-combination-in-my

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!