I'm trying to do a simple update query in MySQL but it doesn't work:
UPDATE dimensions
SET is_active = 1
WHERE eco_tax = 19.2;
eco_tax is a FLOAT type column and it seems that here is the problem, because when I try updating with an INT column it works.
So what cand I do to use a float in column in my where syntax in MySQL?
I always encounter issues when i want to do WHEREs in databases, and it is most likely a issue with floating point math. I know that doubles work the same way, but for some odd reason, it always works with using doubles. Therefore, my suggestion to you is to change the datatype to double instead.
Use double instead of float. It seems there has a bug of mySql. See the following link for more information.
http://bugs.mysql.com/bug.php?id=14268
The root of this problem is that some numbers cannot be represented exactly in floating point. You could try something like the following (unfortunately, I don't have access to a MySQL instance to hand to try this myself):
UPDATE dimensions
SET is_active = 1
WHERE ABS(eco_tax - 19.2) < 1E-08;
In other words, update if the difference between the two values is negligible.
try to alter the column to DECIMAL(x,y).
I had the same issue and I discovered it happens only on the rows when a floating value is used in the where clause; I changed the column type from FLOAT to DECIMAL(5,2) (of course choose your scale and precision) and... no more problems!.
Related
I'm using phpmyadmin 4.0.10.7 on my server and I have a float column that sometimes doesn't work as itended: it is set as default: 0, but sometimes the value is entered as 0.00000000953674 when a new row is added.
Why is this happening?
Floating point numbers are not stored as exact values. See MySQL's writeup on the issue here.
Unless you have a specific need for floating point precision, I would recommend switching to the DECIMAL type, which does not have this problem (as of MySQL 5.0.3). If you must use floats, just remember that the values will not be exact, and direct comparisons will not always work (i.e. WHERE myVal = 5). Instead you should do ranged comparisons (i.e. WHERE myVal > 4.999 AND myVal < 5.001).
Is it possible to find the min value of a column of floating numbers using a mysql function? Suppose I have the following table:
id | value a | 24.88 a | 119.99
If I try:
SELECT MIN(value) FROM [table name] GROUP BY id;
mysql returns:
119.99
After testing this with different floating numbers I believe that this is the case because mysql takes the first character in each of the strings "1" and "2" and then selects a min based on which character is smaller.
I've read through this forum and others trying to find an answer but it seems nobody has raised this problem.
I should mention I've also tried CEIL(value) but that function also seems to have some bugs and I'd prefer to keep the number a floating number and not an integer.
Thanks everyone.
It looks like the column is being stored as a character-based data type. You can solve this in one of two ways:
Change the column type to a numeric type
change the query to add CAST around the value: MIN(CAST(value AS DECIMAL))
The column change might look like this:
ALTER TABLE my_table MODIFY COLUMN value double;
And, as far as I know, MySQL will attempt to convert the data for you. See the note here, which states it "tries".
I'm trying to store float values in MySQL and my values seem to keep getting messed up. :(
I have my fields defined as float(10,7) and I round my values properly in PHP before inserting them:
$rndval = round($val,7)
INSERT INTO mytable (float) VALUES ($rndval)
But when I insert a value such as 47.5206797, it shows up as 47.5206795 in my table. Why is that?
If you need a value to be precise, store it as an exact data type such as DECIMAL(17,7), which would provide the same range as FLOAT(10,7). The only down side is that the DECIMAL will take up more disk space than the equivilant FLOAT, however this is trivial compared to correcting for floating point errors where precision is a concern.
http://dev.mysql.com/doc/refman/5.0/en/fixed-point-types.html
For more information on floating point number issues, the following may be worth a read
http://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html
I've got a column for storing float data, i.e.
1.1
11.60
4.23
Unfortunately, 11.60 gets stored as 11.6. I need it to have that extra zero. Do I have to change my datatype to varchar? What's the best way to handle this?
It sounds from the comments that you're storing a product code, so float isn't a good choice for a datatype, as you suggest. Indeed it's not a rendering issue, but we'd misconstrued it from your initial choice of float (thinking you indeed were storing something like money or true decimal).
Go with varchar, as you suspected, as it really is a string value.
Here's how you can do that:
create a new column of type varchar(100) or whatever length is suitable for you
copy the values into the new column from your float column
ALTER TABLE MyTable ADD MyNewColumn VARCHAR(100);
UPDATE MyTable
SET MyNewColumn = FORMAT(MyFloatColumn, 2);
This is a rendering issue, not a data issue. To "solve" it, apply mysql's FORMAT function to your value as you select it:
select FORMAT(my_float_column, 2)
from my_table;
The 2 is the number of decimal places. It will handle (almost) any number of digits to the left of the decimal place.
This maybe an easy one but i couldn't get answer.
I need to select float value from table
example table :-
value
10.2
4.5
4.6
4.06
my query
SELECT * FROM table where value = '4.6'
returns empty result set
how can i solve this !
Generally, you should never check equality with floats (unless, potentially, you have the same object). Internally, it is represented with more precision, even if it isn't showing it to you by the time it outputs to the screen. This basic tenet holds true for computing in general.
There are a dozens of schemes for doing this, but here is a simple one, which should make sense:
SELECT * FROM table where value BETWEEN 4.599 AND 4.601
Use decimal instead of float.
A decimal(10,2) will have 2 and only 2 decimal places and can be compared in the same manner as integers.
Especially for monetairy values you should always use decimal, but anywhere where rounding errors are unwanted, decimal is a good choice.
See: http://dev.mysql.com/doc/refman/5.1/en/precision-math-decimal-changes.html
Or
MySQL DECIMAL Data Type Characteristics
Today, I also came across the same situation and get resolved just by using FORMAT function of MySQL, It will return the results that exactly match your WHERE clause.
SELECT * FROM yourtable WHERE FORMAT(`col`,2) = FORMAT(value,2)
Explanation:
FORMAT('col name',precision of floating point number)
Hope it helps.
You can also try query
SELECT * FROM table WHERE value LIKE 4.6;
you write:
SELECT * FROM table where round(value, 1) = 4.6