MySQL ERROR 1264 (22003): Out of range value for column - mysql

I get this error:
ERROR 1264 (22003): Out of range value for column 'median_comments' at
row 1
after running this query:
update influencers set `median_comments` = 1347 WHERE `id` = 1;
I'm not sure why this fails on this number which doesn't have any decimals and which is only 4 digits.
The field type is:
median_comments decimal(10,8)

You are using DECIMAL(10,8) that means max digits before decimal would be (10 - 8) = 2.
Reference: DECIMAL Data Type Characteristics
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments are as follows:
M is the maximum number of digits (the precision). It has a range of 1 to 65.
D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.
To fix the error, change your datatype to DECIMAL(10,2).
ALTER TABLE `influencers`
CHANGE COLUMN `median_comments` `median_comments` DECIMAL(10,2) NOT NULL DEFAULT 0;

If you are using decimal(10,8) as data type it means you are specifying 8 digits after decimal place which leaves only (10 - 8 i.e 2 digits) for your whole number.
In this case since your number 1347 contains 4 digits (whole number) hence you are getting the error as "Out of range value" since you are allowed only 2.
You should consider changing it to at least decimal (12,8) which will leave you 4 digits for your whole number part and your above command should work.
Please refer to post - Number format issue in Oracle. Same issue.

As you like me came here from google and your issue is related to Doctrine, and your column type is type="decimal", then you should configure precision and scale properties of your column in the right way.
For me, it was like before:
/** #ORM\Column(name="revenue", type="decimal", scale=4, nullable=true) */
private $revenue;
after
/** #ORM\Column(name="revenue", type="decimal", precision=14, scale=4, nullable=true) */
private $revenue;
It will be converted to DECIMAL(14,4), which means fourteen digits total, four of which are to the right of the decimal point.
Don't forget to prepare migration and run it to apply the changes.
Finally, you should get SQL like this:
ALTER TABLE project_finance CHANGE revenue revenue NUMERIC(14, 4) DEFAULT NULL

Related

SELECT on a varchar field that has text and numbers

I have a field remote_number, which is Varchar(30).
Most data is a number ranging from 3 to 11 digits long.
When a call is anonymous, this fields value is set to 'anonymous'.
I need to filter data from this field where the value is either >999 OR anonymous.
I can do these queries independently, for example,
SELECT * FROM call_history WHERE remote_number>999;
or
SELECT * FROM call_history WHERE remote_number='anonymous';
When combining the 2, such as
select * from call_history where (remote_number>999 OR remote_number='anonymous');
All data that had anonymous in the remote_number field is truncated due to not being a double.
Warning | 1292 | Truncated incorrect DOUBLE value: 'anonymous'
How can I adjust this query so that the 'anonymous' data is not truncated?
Edit:data type1
query ran
Warning
The reason this does not work, is if we say remote_number>999 we are implying that remote_number is a number but it's clear the returned value is a string not a number.
To fix I specified char length.
AND (char_length(remote_number)>3 OR remote_number='anonymous'

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

MySQL Update command

I have a query in MySQL
UPDATE `mylandho_foreclosure`.`property_commercial`
SET `winningBid` = '14000000.00'
WHERE `property_commercial`.`propertyId` =325 LIMIT 1 ;
but it shows warning
Warning: #1264 Out of range value adjusted for column 'winningBid' at row 1
datatype is float(9,2)
Float(9,2) would allow for 7 numbers before the decimal, and 2 after, you have 8 before and 2 after.
You need to increase the size of the filed if you wish to change the value to what you require.
You should have a look at Numeric Types
For example, a column defined as
FLOAT(7,4) will look like -999.9999