I want to remove trailing line breaks from my MySQL column. trim() only removes whitespaces but I also want to remove trailing linebreaks. Could anyone suggest?
Just ran into this problem, and took care of it like this.
UPDATE table_name SET col_name = REPLACE(TRIM(TRAILING ' ' FROM col_name),
TRIM(TRAILING '\r' FROM col_name),
TRIM(TRAILING '\n' FROM col_name))
This will remove new lines (\n), carriage returns (\r), and whitespace.
Try something like this:
REPLACE(FIELD,'\r\n',' ')
You can replace these directly from SQL by matching "\r" at the end, then replacing that "\r".
Example:
UPDATE Person SET firstName = REPLACE(firstName, '\n', '')
where firstName LIKE '%\n'
or
UPDATE Person SET firstName = REPLACE(firstName, '\r', '')
where firstName LIKE '%\r'
Another vote for Wallack's idea, but more specifically:
Update YOURTABLENAME set YOURCOLUMNNAME = replace(YOURCOLUMNNAME, '\r\n','')
Essentially this should replace carriage returns and breaks with nothing.
Good luck.
This will remove all escape characters:
TRIM(TRAILING '\\' FROM (REPLACE(REPLACE(REPLACE(column_name, '\n', ' '), '\r', ' '), '\\', ' ')))
Based on EternalHour's answer:
UPDATE TABLE_NAME SET `COLUMN_NAME` = TRIM(TRIM(TRAILING '\r' FROM TRIM(TRAILING '\n' FROM TRIM(`COLUMN_NAME`))))
this one removes trailing \r\n
one by one, so either it is a newline or a carriage return or both, it will be stripped, and also removes whitespaces before and after the process.