Why does my SELECT not ignore whitespace, and why does TRIM seem to have no effect?
Here are the partial results of something like SELECT DISTINCT crop_year from raw_data
:
Rows crop_year
105755 '2010'
12326 '2010 '
256363 '2011'
319321 '2011 '
...
I typed in the single quotes to illustrate the fact that there is trailing whitespace.
I have 2 problems here on my production server... which I cannot replicate locally:
A. I expect SELECT
to ignore whitespace, as explained in numerous other questions, as well as the MySQL Docs, but it clearly does not work this way on my prod server.
B. I expect UPDATE raw_data SET crop_year = TRIM(crop_year)
to fix the problem, but running this query results in 0 affected rows.
Other background: This column type is VARCHAR(11). If it's relevant, the table contains mixed storage engines and collations: this table is MyISAM, and this column is currently latin1_swedish_ci.
PS: In this specific case, year is always a 4 digit value, so I eventually changed the column from a VARCHAR(11) to CHAR(4)... which effectively trimmed the whitespace, but I am still posting the question because I find it likely that I will encounter a similar problem on other columns which are not of a fixed length.