mysql min value column of floating numbers - mysql

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".

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;

Access adds extra decimals when grouping in query

Did not found anything to solve this.
I'm doing some easy things on an Access Database 2007 32 bit.
Got this table:
Id_iva Desde Hasta Valor_Iva
2 01/01/2000 31/08/2012 18,00%
4 01/09/2012 31/12/2021 21,00%
5 01/01/2022 31/12/2099 25,00%
Valor_iva is a numeric field, single type. I manually input those numbers, with 2 decimals only (in this case all of them are 0, but it could be something like 18,50% or 20,23% and so on)
If I make a query like this:
SELECT T_IVA.Hasta, T_IVA.Valor_Iva FROM T_IVA;
It works as expected and it returns exactly the values:
But If my query is this:
SELECT T_IVA.Hasta, Sum(T_IVA.Valor_Iva) AS SumaDeValor_Iva FROM T_IVA GROUP BY T_IVA.Hasta;
I get extradecimals in some values.
Can't understand where those decimals come from.
I've googled about CAST and TRUNCATE but I could not apply those (or I don't know how to).
WHAT I WANT: I just want to make a GROUP BY query that does not add those decimals.
Thanks in advance.
If you want exact results, then cast to an exact type before doing any operations. Or, even better, use an exact (non-floating point) type in the first place.
It seems your values fit in the Currency data type. The Decimal data type can be used for larger values with decimals.
SELECT T_IVA.Hasta, Sum(CCur(T_IVA.Valor_Iva)) AS SumaDeValor_Iva FROM T_IVA GROUP BY T_IVA.Hasta;

How to change the field type of result set from SQL pass-through (SPT)?

I have a mysql table column defined as unit_price float(12,4).
If I assign a value of 0.1234, when I create a remote view with dbsetprop, I can retrieve 0.1234 with myview.unit_price.
However, when I use SQL pass-through (SPT) like this:
sqlexec(nHandle, "select * from table", "oResult")
the result of oResult.unit_price only shows 0.12.
How can I ensure that I return the correct, full value?
Not having explicitly tried, you can do some simple math forcing to create larger precision, such as adding an additional column to your query... something like...
select *, unit_price * 1.00000 as UnitPrice5 from...
This will force the field to be computed to 5 decimal position and MIGHT actually change the result column to properly handle this forced decimal capacity. Then, you would use the "UnitPrice5" column instead of "unit_price"...
Don't know if that would be a big issue for you, but once it is in VFP, you have more control too.
The length of the decimal values are defined in your FoxPro settings. Go to Tools->Options->Regional and set the Decimal Digits to a higher value.

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.

mysql float data not selecting in where clause

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