How to insert/update decimal into mysql via PHPmyadmin? - mysql

I want to insert/update decimal number to mysql. But everytime I did. It return the round number or truncate dot number. I tried change the datatype of lv_pay and lv_dis either to decimal and double but still the result.
MySQL
update settings_price_pay set lv_pay='3.2',lv_dis='0' where pset='1' and cate='161a5954c2e7713417906c523204a2be' and ltype='p_rhi'
PHPMyadmin

First:
The data type of those numeric fields should be DECIMAL(12,2) or something similar, declaring that you use a picture of S#########9.99. Sign, ten digits, point, two digits.
Second:
Don't put your numbers in 'quotes'. If you do, MySQL first coerces them to IEEE 64-bit numbers, then to whatever datatype you have for your columns. Say this:
set lv_pay=3.2, lv_dis=0
Notice that MySQL ignores the numbers in parentheses in DOUBLE(11,2) and simply uses a 64-bit IEEE floating point number. (It honors those numbers when you declare a DECIMAL(12,2) data type.)

Got an answer, just for novice like me. Change the 'length/value' of the row (in my case) from (11,0) into (11,2). Found it accidentally.

Related

What is the correct default value for a MySQL decimal field?

I have a decimal field in my MySQL database. I have defined it as this:
decimal(1,1) UNSIGNED NULL. But I would like to set a default value for it like 7.0, and this is the problem I have. Whenever I want to set this value, I get this error:
Invalid default value ...
I also tried to set it as 7,0 and 7 but it resulted the same error. What is the correct default value for a MySQL decimal field?
Note: I am using Navicat for MySQL
In MySQL, when declaring DECIMAL(P,S) :
The precision (P) represents the number of significant digits that are stored for values, and the scale (S) represents the number of digits that can be stored following the decimal point.
So in your example, DECIMAL(1,1) means at most 1 digit, and at most 1 digit after the dot... which doesn't really make sense.
To better understand, here are more examples:
DECIMAL(5,2): 5 digits, two of them being used for the fractional part. Hence, possible values range from -999.99 to 999.99
DECIMAL(5,0): no fractional part allowed, so it is equivalent to an integer with maximum 5 digits.
With UNSIGNED, the behavior is the same, but using a minus sign will throw an error.

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 datatype for numbers maximum character length 19 precison 17

Was wondering if someone could help me on a data type questions for mysql. I would like to be able to load in numbers with maximum character length of 19 but with a maximum precision of 17 (so can have 17 digits after the decimal). I have tried using float(19,17) and decimal(19,17) but when i try loading a number such as 02.11111111112222222 it will not capture the full number. Thanks in advance
You can use decimal
decimal(19,17)
https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html
Mysql Will store this value as 2.11111111112222222 i.e. without the leading 0.
check mysql documentation for decimal type, it says:
DECIMAL columns do not store a leading + character or - character or leading 0 digits. If you insert +0003.1 into a DECIMAL(5,1) column, it is stored as 3.1. For negative numbers, a literal - character is not stored.
The leading 0 case applies to your value and mysql is not storing that. Hence, you are getting 2.11111111112222222 stored in the table.
check https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html
If you want to store leading zero as well change the definition by including UNSIGNED ZEROFILL after the datatype:
CREATE TABLE your_table
(
your_column DECIMAL(19,17) UNSIGNED ZEROFILL
);
Just to make a note:
if you are entering 2.11111111112222222 you will get a zero padded to make the digit count 19-17 for before decimal point and get 02.11111111112222222
if you are entering 02.111111111122222 you will get zero padded in right to make the digit count after decimal point as 17.

Decimal datatype is rounding the values

I have set my MySQL field table data type to the decimal because from what I have read, I would be able to store the price with commas/dots in the decimal data type fields... The problem is that whenever I store any data with the comma or dot, MySQL is rounding it automatically up or down. Eg. When I'm executing the following query:
UPDATE table SET field = 114.21 WHERE id = 1;
Then field is set, but the value is rounded to 114, instead of displaying the data I set in the query (114.21) - is there any solution for that? Or I should just use other data type?
AFAIK the dot is the standard notation for decimal values. Using Commas may trigger SQL parse errors or may go unnoticed if the syntactical context allows for a comma to be there.
How did you define the precision of the DECIMAL column?
If it is DECIMAL(10, 2) it will have a total of 10 numbers of which 2 are decimal values (with 2 decimal rounding meaning that 10.215 is saved as 10.22 and 10.214 becomes 10.21).
If it is DECIMAL(10) it will not have any decimal values and be rounded to an integer.
If you use FLOAT or DOUBLE PRECISION you don't have to specify the number of decimal values but it has its own flaws.
As Mihai mentioned you need to define the proper precision for the decimal type, e.g. DECIMAL(10,2) for two decimal places.
When inserting a decimal value mySQL will round.
From the docs:
For inserts into a DECIMAL or integer column, the target is an exact data type, so rounding uses “round half away from zero,” regardless of whether the value to be inserted is exact or approximate.
See http://dev.mysql.com/doc/refman/5.0/en/precision-math-rounding.html for details.
Well before I have also an issue regarding on what to use on my numbers with decimal points. But problem solved by using DOUBLE(10,2) as my DATATYPE, and it shows the exact number on the database when you save it. Hope it will help.

MySQL field type for storing decimals

I'm creating a DB that will hold products with several "height" columns (in meters, for ex 7.79 or 12,8). Never more than 2 digits before and 2 after the decimal point. What field type should I use for this?
If I use decimal(2,2) an try to insert 7.79 in phpmyadmin I get an error saying Warning: #1264 Out of range value for column 'working_height' at row 1
I'll be using this DB for searching, so I have to be able to run a query like "select all products where height is great than 7".
You're looking for decimal(4,2) - in general, decimal(m,n) means m total digits, and n to the right of the decimal point. Docs here.
So a decimal(2,2) can store two total digits, both to the right of the decimal point. This explains the error that you are seeing.
People will say to use decimal(s, d) but how about storing the values as integers, in centimeters instead of meters? Easier to compare (no precision loss).
Just my two cents.
Try DECIMAL(4,2) instead
Refer to: MySQL Numeric Types