Mar 30 2010

MySQL: SELECT not working on TRIM queries – a workaround

We recently noticed that MySQL does not work for WHERE queries with TRIM like:

UPDATE tablename SET column = TRIM(column) WHERE column != TRIM(column)

Technically a query like this is unnecessary, you can just TRIM directly.

But say you wanted to do a SELECT since you need to do something else with those records. This does not work:

SELECT * FROM tablename WHERE column != TRIM(column)

We found an simple workaround for this this MySQL shortcoming. Just use:

SELECT * FROM tablename WHERE CHAR_LENGTH(column) != CHAR_LENGTH( TRIM(column) )

  • Share/Bookmark