Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Removing trailing linebreaks from a column

Tags:

mysql

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?

like image 279
Jeets Avatar asked Jun 11 '13 12:06

Jeets


2 Answers

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'
like image 70
jakob Avatar answered Nov 03 '22 09:11

jakob


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.

like image 21
EternalHour Avatar answered Nov 03 '22 09:11

EternalHour