views:

4607

answers:

8

From my experiments, it does not appear to do so. If this is indeed true, what is the best method for removing line breaks? I'm currently experimenting with the parameters that TRIM accepts of the character to remove, starting with trimming \n and \r.

A: 

select trim(both '\n' from FIELDNAME) from TABLE;

Jason Lepack
This does not work as it appears the newline in MySQL is not the \n.
Thomas Owens
Ah, well. Cheers!
Jason Lepack
+2  A: 

Trim() in MySQL only removes spaces.

I don't believe there is a built-in way to remove all kinds of trailing and leading whitespace in MySQL, unless you repeatedly use Trim().

I suggest you use another language to clean up your current data and simply make sure your inputs are sanitized from now on.

Peter Crabtree
Yes, new inputs are sanitized, but dealing with old data is...well...not fun.
Thomas Owens
Ha, of course. But if you've got a list of fields which must not have trailing or leading whitespace, you should be able to write a one-off program that will fix up your current data -- 99% of programming languages have a Trim() that will do what you're looking for.
Peter Crabtree
A: 

select trim(both '\r\n' from FIELDNAME) from TABLE; should work if select trim(both '\n' from FIELDNAME) from TABLE doesnt work;

A: 

i could only get it to work by making the char;

trim(both char(13) from fieldname)

+1  A: 

My line breaks were in the middle of the string, and I didn't have control over the source data. The following mysql command worked for me:

REPLACE(FIELD,'\r\n',' ')
David Hitchen
A: 

REPLACE(FIELD,'\r\n',' ') works perfectly on MySql 5.1 database - thanks!

boardtc
A: 

The answers above, when combined, work. A full example of replacing linebreaks at the beginning and end of the field looks like this:

UPDATE table SET field=REPLACE(field, field, TRIM(BOTH '\r\n' FROM field))
Ilya
A: 

I faced the same issue with one of the fields. There is no perfect solution. In my case i was lucky that the length of the field was supposed to be 6. So i used a query like

update events set eventuniqueid = substring(eventuniqueid, 1, 6) where length(eventuniqueid) = 7;

You will just have to choose the best option based on your need. The replace '\n' and '\r\n' did not work for me and just ended up wasting my time.

Punit Raizada