Question

I have tried using the TRIM() in MySQL to delete all of the whitespace in every field in a column but it will only remove it from the front. I have long strings that have spaces in the middle next to hyphens and I need the space between the hyphens removed. Is there a way to do this?

UPDATE FOO set FIELD2 = TRIM(FIELD2);
Was it helpful?

Solution

Use the REPLACE() method, which can replace every occurrence of a character by another:

UPDATE foo SET field2 = REPLACE(field2, ' ', '');

This query will remove every spaces in the field2 column.

Definition of the method, from the MySQL doc:

Returns the string str with all occurrences of the string *from_str* replaced by the string *to_str*. REPLACE() performs a case-sensitive match when searching for *from_str*.

mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww'); -> 'WwWwWw.mysql.com'

This function is multi-byte safe.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top