After a database import, there's some rows with dirty fields that end in (sometimes multiple) spaces saved in the database, and in the interest of finding them amidst the many thousands of other rows, I'm trying to do a query like:
SELECT * FROM `mytable` WHERE `dirtyfield` REGEXP ' $'
But that returns zero rows. I tried a few other variations with other results:
SELECT * FROM `mytable` WHERE `dirtyfield` REGEXP '[[:space:]]$' -- Zero Rows
SELECT * FROM `mytable` WHERE `dirtyfield` REGEXP '[[.space.]]$' -- Zero Rows
SELECT * FROM `mytable` WHERE `dirtyfield` REGEXP '[[.space.]]' -- Those with a space anywhere in the value
SELECT * FROM `mytable` WHERE `dirtyfield` REGEXP '[[.space.]]{2}' -- Those with two spaces in a row
Finding all with a single space doesn't help much, since some clean rows have single spaces between words in that field. That last one catches 90% of the dirty rows, but misses those that have just a single space at the end. Is there something wrong with how I'm using the $ symbol to indicate the end of a field?
The MySQL RIGHT() and SUBSTRING() functions seem to strip off whitespace when calculating the end of the field:
SELECT * FROM `mytable` WHERE RIGHT(`dirtyfield`)=" " -- Only returns one row that has " " for that field
SELECT * FROM `mytable` WHERE SUBSTR(`dirtyfield`,-1)=" " -- Only returns one row that has " " for that field
One other try using a comparison didn't work either:
SELECT * FROM `mytable` WHERE TRIM(`dirtyfield`)!=`dirtyfield` -- zero rows returned
The "dirtyfield" field is a VARCHAR(128), if that matters.
EDIT: I'm an idiot; the fields don't end in spaces, the fields end in multiple spaces followed by a comma (imported from a bad CSV file).
SELECT * FROM `mytable` WHERE RIGHT(`dirtyfield`,1)=','
That query found them. I was looking at the output of the tables in a comma-separated view and didn't notice the commas were doubled-up.