mysql float data not selecting in where clause - mysql

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

Related

MySQL-query to retrieve MAX-value doesnt work for decimal numbers

I have a MySQL table that looks like this:
id layer l_to blank
1 1 10 xyz
0 0 5.5 xyz
I want to get the highest number of column-variable "l_to" that shares column-variable "blank".
I have tried the following SQL-query:
SELECT MAX(l_to), COUNT(layer),l_from FROM layers WHERE blank='xyz'
This works fine, if "l_to" of layer 1 is below 10. If it is ten, the query returns "l_to" from layer 0 (5.5).
Any Idea for why this is, and how can I retrieve the MAX?
#EDIT: Changing Datatype of "l_to" from VARCHAR to DECIMAL (5,1) got me the desired result. Thanks for the answers!
The datatype is not a number for field l_to so 5 is greater than 1 for a string. Probably a varchar. Change field l_to to a Decimal [1].
Only consider casting if you do not have control over the table structure as best practice is the data type reflects the data use in the world. This protects the data integrity of the database, provides helpful functions related to the datatype and ensures intuitive outcomes, like Max function. Casting as a work around for this query will only lead to downstream issues; refactor the structure now if you can.
Reference
Decimal data type suggested in comments by #Akina. Originally suggested float, but Decimal appears to reflect the Use Case better than float, given the limited examples shown.
Cast l_to from string to decimal
SELECT MAX(cast(l_to as DECIMAL(10,2)), COUNT(layer) from FROM layers WHERE blank='xyz'
Use a LIMIT query, and also cast the l_to column to decimal:
SELECT *
FROM layers
WHERE blank = 'xyz'
ORDER BY CAST(l_to AS DECIMAL(12.4)) DESC
LIMIT 1;

How do I show mysql value only after decimal

Is there any mysql function that helps to display value only after . Suppose if I have a column which has value 45.23 I want only to select .23 as value . The example table can be
test_table
45.23
One method is:
select col - floor(col)
(You might want to tweak this if you have negative values, using abs(col - truncate(col, 0)).)
MySQL also supports the more concise:
select col % 1
To be honest, though, I've never liked this convention (useful as it is) probably because of my background in discreet mathematics.
The RIGHT works as well. Consider this option.
create table numbers(a float);
insert into numbers values (45.23)
select right(a,2) as rem from numbers
db<>fiddle

Mysql where clause comparison give different result for double and float data type for same value

My mysql version is 5.7.14
I have 1 table with two column
1). price_val_float with float data type
2). price_val_double with double data type
Table structure
CREATE TABLE test (
price_val_float FLOAT(6,2),
price_val_double DOUBLE(6,2)
);
Same value in both column
INSERT INTO test VALUES
(78.59, 78.59),
(78.60, 78.60),
(78.61, 78.61);
Now I set one variable as follow
SET #priceValue=78.6;
Now I want to get all record from test table where price_val_float >= #priceValue;
SELECT price_val_float FROM test WHERE price_val_float>= #priceValue;
above query return only 78.61
But if I run same query of price_val_double column
SELECT price_val_double FROM test WHERE price_val_double>= #priceValue;
This return
78.60
78.61
I am not getting why mysql return different result as only data type is different.
Does anyone knows about this ?
Here is Fiddle for testing
Thanks in advance.
This might sound strange to say but this is because decimal numbers are approximates values. This is an issue across all programming due to the nature of storing large numbers. Even the mysql documentation calls these "approximate" values:
https://dev.mysql.com/doc/refman/5.5/en/floating-point-types.html
For example: MySQL performs rounding when storing values, so if you insert 999.00009 into a FLOAT(7,4) column, the approximate result is 999.0001.
This is explained in the mysql documentation here:
https://dev.mysql.com/doc/refman/5.5/en/problems-with-float.html
Or as an additional case explained in Python here:
https://docs.python.org/2/tutorial/floatingpoint.html
The way to get around this is identify the precision you want and store the value as an integer.
Float is a single precision and Double is for double precision that why your getting the difference.
This is happening because the difference between the numbers shows up around the tenth decimal or so, depending on factors such as computer architecture or the compiler version or optimization level. For example, different CPUs may evaluate floating-point numbers differently.
You need to use DECIMAL data type for more accurate results. Also check this for more details
That is because Float point values are not stored as exact values. If you need exact value you can use Decimal data type. You can read about it here

Mysql numbers in varchar columns query issue

I have this column in a talbe which for historical reason is a string but nowdays new values are put in are numbers.
I have to find the biggest value from an interval of numbers but the values are string in the DB :(
If op_calcul would have been a number it would have been trivial sql:
Select MAX(op_calcul) FROM nom_prod where op_calcul >=500 and op_calcul < 5000
but having them as varchar put me into some trouble.
Tried different scenarios but did not come with a solution yet.
Any hint ?
Thanks
SELECT MAX(CAST(op_calcul as SIGNED)) FROM nom_prod...
Should do the trick. You might need to cast the WHERE clauses as well.
Ideally though, for performance reasons on both the MAX and the WHERE, you should use an integer column. Is there non-numeric or out of range data in it or something? It should be fairly trivial to change the column type if not.
use mysql cast to convert data into required datatypes.
http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_cast
Note however that it does affect query performance.
SELECT MAX(CAST(`op_calcul` AS SIGNED))
FROM `nom_prod`
WHERE `op_calcul` >= 500
AND `op_calcul` < 5000;

MySQL - avoid truncating trailing zeros in float datatype

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.