Out of range decimal type Mysql? - mysql

I need to store int and floating values numbers in field.
I use this type for field: decimal(5,4).
When I tried to store number 10 I got an error: Out of range decimal.
Why, if decimal(5,4) allows 5 position before dot and 4 positions after?

In the decimal(x, y) format, the x stands for the total number of digits, and y for the number of digits after the decimal place (. or ,). If assuming you want to store numbers up to 4 digits long (1000) with a precision of 2 (0.01) you'd need to use decimal(6, 2).

You are misunderstanding. decimal(5, 4) has 5 total digits of precision with 4 digits of scale after the decimal place. Hence, one is before.
You seem to want decimal(9, 4).

Related

Get values from database with only 2 decimal places using MySQL

I want to retrieve values from a table with figures formatted in float. The decimal places range up to thirteen. I only want to retrieve the rows whose values only have exactly 2 decimal places. How do I do this?
Expected output:
[45.678, 56.236656457, 89.23, 100.89] ==> [89.23, 100.89]
You can use select Length(123.12 % 1) - 2 to get the length of the values after the decimal.
select (123.12 % 1) returns 0.12 so you always know that you have 0. in your result set, you can then get the length of your value minus the two characters and only select the rows where length = 2.

Getting SUM of MySQL table data when there is floating point numbers

I an using MySQL database and I have a table called fertilizer_storage which is using both plus and minus values. It has 4 columns uria,TSP,MOP and TDM
I am using double as data type and getting sum of each column using follwing syntax,
SELECT SUM(uria),SUM(TSP),SUM(MOP),SUM(TDM) FROM `fertilizer_storage` WHERE `branch_ID`=1
The problem is for some columns I get unwanted floating points of 15 while all columns are containing numbers up to 4 floating points.
7.666900000000002
7.666900000000002
9.6109
9.9924
when I changed numbers in first two columns as other two it gives the correct answer. what should I do to correct this.
You can use ROUND(X, D):
Rounds the argument X to D decimal places. The rounding algorithm
depends on the data type of X. D defaults to 0 if not specified. D can
be negative to cause D digits left of the decimal point of the value X
to become zero.
SELECT Round(Sum(uria), 4),
Round(Sum(tsp), 4),
Round(Sum(mop), 4),
Round(Sum(tdm), 4)
FROM `fertilizer_storage`
WHERE `branch_id` = 1
See it in action

Cast as decimal in mysql

I have below table structure and data :
create table sample
(
id INT(10)
);
INSERT INTO sample
values
(23398),
(98743),
(54734);
Now I want to understand CAST function in mysql. Consider following query :
select
cast((id/3) as decimal(2,2)) as cast1,
cast((id/3) as decimal(3,2)) as cast2,
cast((id/3) as decimal(4,2)) as cast3,
cast((id/3) as decimal(5,2)) as cast4,
cast((id/3) as decimal(6,2)) as cast5,
cast((id/3) as decimal(7,2)) as cast6,
id/3 as actualId
from sample;
Please see output of this query at SQL Fiddle.
I am wondering why this query gives 0.99, 9.99 and vice versa.
Can anyone explain it ?
Thanks in advance.
decimal is a type that takes 2 arguments
decimal(size, places) :
size determines how many digits are in the number.
places determines how many of those digits are to the right of the decimal.
decimal(2,2) - .00 - 2 digits both of which are to the right of the decimal
when casting (23398 / 3) = 7799.33333333 to declimal(2, 2) it yields a decimal in the specified amount of space closest to the desired number which is 0.99
decimal(3,2) - 0.00 - 3 digits 2 of which are to the right of the decimal
when casting (23398 / 3) = 7799.33333333 to declimal(3, 2) it yields a decimal in the specified amount of space closest to the desired number which is 9.99
if all of the original numbers were negative you would yield -0.99 and -9.99 because they are the closest numbers to the desired number within the allocated space
As a matter of fact java does something similar if you take the max double and try to convert it to an int you will give the max int which is no where near the max double

mysql insert/update converts a number

I have two mysql columns both int unsigned zerofill. The first 5 in length, second 11 in length. First value takes any 5 digit number no problem. The second, no matter what converts any 11 digit number into 04294967295. Any clue on what I can do to solve this puzzle?
Your number is larger than the integer field can handle - 232 - 1.
Change the column to an unsigned BIGINT and you'll be good up to 18,446,744,073,709,551,615.
The length you give to an integer field is just for displaying. If you pass a length 11, a select commando on that column will display a number of 11 digits (which is the length of the number you got). The actual size of the field is determined by the type you chose for the column (int). int-fields are capable of storing 232 different values. For an unsigned field having 0 as its first value, this results in a maximum value of 232 - 1, which matches the number in your output. Since any 11 digit number you input will be bigger than this number, the value saved will be 232 - 1.
You can overcome this limitation by using the BIGINT-type, which allows you to store any number up to 264.
Range of UNSIGNED INT is 0 to 4294967295. so Any value above 4294967295, it will insert that max value, as that is the maximum value possible for unsigned INT.

Anomalous mysql behaviour on replace query

im using a v simple database and i have 3 columns A(bigINT 20) , B(bigInt 20) and c(DECIMAL(5,4)) , when i fire the following query i get the below mentioned results :
REPLACE INTO `my_table` SET `A` = 8,`B` = 44,`C` = 14;
i get these values in mysql A =8 , b= 44 and c as 9.9999 ! ?
any ideas as to why is this happening and what can i do to resolved this ?
DECIMAL(5,4) means that the number has at most 5 digits, 4 of them after decimal point. So 14 is simply overflow as it would require DECIMAL(6,4).
It must be cleared that 14 is overflow, because as constant precision point decimal it is internally 14.0000 here (so six digits over five).
So if you try to put 14.0000 (six digits) in DECIMAL(5,4) (five digits max) -> MySQL chooses value closest to the one you request. Therefore 14.0000 gets "rounded" to 9.9999.
To fit 14 in your column you can either extend it do DECIMAL(6,4) (to allow more digits in general) or change to DECIMAL(5,3) (which will allow one more digit before decimal point, but loses some precision of course).