I can use the MySQL TRIM()
method to cleanup fields containing leading or trailing whitespace with an UPDATE
like so:
UPDATE Foo SET field = TRIM(field);
I would like to actually see the fields this will impact before this is run. I tried this but returns 0 results:
SELECT * FROM Foo WHERE field != TRIM(field);
Seems like this should work but it does not.
Anyone have a solution? Also, curious why this does not work...
The TRIM() function returns a string that has unwanted characters removed. Note that to remove the leading spaces from a string, you use the LTRIM() function. And to remove trailing spaces from a string, you use the RTRIM() function.
A leading space is a space that is located before the first character (letter, number, punctuation mark) in a text entry field. A trailing space is a space that is located after the final character in a text entry field.
Trailing spaces were removed in both cases. With MySQL 5.0 however things changed so now VARCHAR keeps trailing spaces while CHAR columns do not any more. Well in reality CHAR columns are padded to full length with spaces but it is invisible as those trailing spaces are removed upon retrieval.
MySQL SPACE() function MySQL SPACE() returns the string containing a number of spaces specified as an argument.
As documented under The CHAR
and VARCHAR
Types:
All MySQL collations are of type
PADSPACE
. This means that allCHAR
andVARCHAR
values in MySQL are compared without regard to any trailing spaces.
In the definition of the LIKE
operator, the manual states:
In particular, trailing spaces are significant, which is not true for
CHAR
orVARCHAR
comparisons performed with the=
operator:
As mentioned in this answer:
This behavior is specified in SQL-92 and SQL:2008. For the purposes of comparison, the shorter string is padded to the length of the longer string.
From the draft (8.2 <comparison predicate>):
If the length in characters of X is not equal to the length in characters of Y, then the shorter string is effectively replaced, for the purposes of comparison, with a copy of itself that has been extended to the length of the longer string by concatenation on the right of one or more pad characters, where the pad character is chosen based on CS. If CS has the NO PAD characteristic, then the pad character is an implementation-dependent character different from any character in the character set of X and Y that collates less than any string under CS. Otherwise, the pad character is a <space>.
One solution:
SELECT * FROM Foo WHERE CHAR_LENGTH(field) != CHAR_LENGTH(TRIM(field))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With