问题
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