I have a few values in the database, in a FLOAT column that I want to search on.
Strangely, values of 2.5 come back when running this:
SELECT * from `prices` WHERE `price` = 2.5
But nothing is returned when I search on 4.8, UNLESS I do a trim on the column before comparing against it, like this:
SELECT * from `prices` WHERE trim(`price`) = 4.8
Anybody know what the cause of this might be, I thought that since it’s a FLOAT field, there shouldn’t be any leading or trailing space that needs to be trimmed. I’m assuming the number of 4.8 isn’t anything special, but it’s still intriguing.
When browsing the database, I can see it as a plain 4.8, with no leading or trailing characters.
I’m a bit stumped as to why this would be happening.
It’s because 4.8 cannot be represented by a float. You can get values very close to it, but not the exact value.
I’m not entirely sure what trim is doing to it (I can’t find anything specifying the behavior maybe it’s not actually defined) but I know that it is changing the value so you can get a match. Read this http://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html if you have further interest on the behavior of floats (although it won’t tell you anything about what trim does to them).