Quoting of decimal values - mysql

I am storing a running total in a Decimal(10,2) field and adding to it as items are processed.
update foo set bar = bar + '3.15'
About 20% of the times a warning is issued "Data truncated for column 'bar' at row 4"
This warning is never issued if the update value is not quoted.
Should decimal values be quoted?

Of course not.
Integers and floats are not strings and should never be quoted. Only MySQL even allows quotes around them.

Is it possible that the value you add exceeds the limits of Decimal(10,2)?
E.g.
update foo set bar = bar + '3.149999'
would cause a 'Data truncated' warning since the field can only store 2 digits to the right of the decimal point (not 6).

No, The decimal values are specified as is. If you quote them it will interpret as a varchar.

No! Quotes are used for strings only, like text, char, varchar, etc

Related

How to insert/update decimal into mysql via PHPmyadmin?

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.

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.

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.

Can char field retain trailing space character?

I have a database table with a primary key called PremiseID.
Its MySQL column definition is CHAR(10).
The data that goes into the column is always 10 digits, which is either a 9-digit number followed by a space, like '113091000 ' or a 9-digit number followed by a letter, like '113091000A'.
I've tried writing one of these values into a table in a test MySQL database table t1. It has three columns
mainid integer
parentid integer
premiseid char(10)
If I insert a row that has the following values: 1,1,'113091000 ' and try to read row back, the '113991000 ' value is truncated, so it reads '113091000'; that is the space is removed. If I insert a number like '113091000A', that value is retained.
How can I get the CHAR(10) field retain the space character?
I have a programmatic way around this problem. It would be to take the len('113091000'), realize it's nine characters, and then realize a length of 9 infers there is a space suffix for that number.
To quote from the MySQL reference:
The length of a CHAR column is fixed to the length that you declare when you create the table. The length can be any value from 0 to 255. When CHAR values are stored, they are right-padded with spaces to the specified length. When CHAR values are retrieved, trailing spaces are removed.
So there's no way around it. If you're using MySQL 5.0.3 or greater, then using VARCHAR is probably the best way to go (the overhead is only 1 extra byte):
VARCHAR values are not padded when they are stored. Handling of trailing spaces is version-dependent. As of MySQL 5.0.3, trailing spaces are retained when values are stored and retrieved, in conformance with standard SQL. Before MySQL 5.0.3, trailing spaces are removed from values when they are stored into a VARCHAR column; this means that the spaces also are absent from retrieved values.
If you're using MySQL < 5.0.3, then I think you just have to check returned lengths, or use a character other than a space.
Probably the most portable solution would be to just use CHAR and check the returned length.
Q: How can I get the CHAR(10) field retain the space character?
Actually, that space is retained and stored. It's the retrieval of the value that's removing the spaces. (The removal of the trailing spaces on returned values is a documented "feature".)
One option (as a workaround) is to modify your SQL query to append trailing spaces to the returned value, e.g.
SELECT RPAD(premiseid,10,' ') AS premiseid FROM t1
That will return your value with as a character string with a length of 10 characters, padded with spaces if the value is shorter than 10 characters, or truncated to 10 characters if its longer.
A standard CHAR(10) column will always have trailing spaces to pad out the string to the required length of 10 characters. As such, any deliberately trailing spaces will be blended in and, typically, stripped by your database adapter.
If possible, convert to a VARCHAR(10) column if you want to preserve the trailing spaces. You can do this with the ALTER TABLE statement.
Though Gordon's answer may still be right by itself, there is on later versions than mentioned a solution.
In your code run SET sql_mode = 'PAD_CHAR_TO_FULL_LENGTH';
With this session setting you'll retrieve perfect columns on full length of the CHAR(10), while VARCHAR does not when no trailing spaces are entered beforehand. If you don't need the spaces, you can always rtrim().

Very large integer inserted into a "TEXT"-type column in MySQL

Just experimenting
I've used this query on MySQL:
insert into table_name (text_col) values (1919191919191919191919191...91919191)
Where that integer is 174 characters long. text_col is of type "TEXT".
I know that integer exceeds a limit, because, instead of that number, I get a 65-long 999..9999 one, but I don't get which limit is exceeded.
I've read here the maximum values, but I still don't get it.
What type of integer is casted if inserted into a TEXT-type column?
Thanks, as always
Edit: I know about inserting that integer as string with single quotes.
Your very long number (without quotes) was converted to Decimal. Since the argument exceeds the capacity for that type (namely, 65 characters), the closest value possible is substituted (all 9's).
You should probably have received a warning on your Insert. Show warnings should explain what happened.
Answer to your question:
You're first telling the compiler to create a number of that size, which it can't really. It's a compiler limit on the size of the number
Optional workaround:
Pass it in as a string, and you should be good to go.