views:

40

answers:

5

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?

+2  A: 

Of course not.

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

Coronatus
e.g. postgresql has an implicit cast rule for string->numeric, too. `SELECT '3.15' + 4.0;` runs fine and returns `7.15`
VolkerK
My gut agrees and I would generally myself not quote any type that represents a number but the quoting is being performed on my behalf by MySQLdb (python).
Peter
A: 

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

Sheff
+1  A: 

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

VolkerK
All the values will be to 2 decimal places as they represent cash. Mysql may be parsing strings as floats and this cause the value to exceed the limits but this is not intended.
Peter
I'd suggest you enable the general log of the MySQL server and check the queries it actually receives. see http://dev.mysql.com/doc/refman/5.1/en/query-log.html
VolkerK
A: 

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

techiesguy
A: 

If you do not quote NUMERIC/DECIMAL values, they will probably first be converted to FLOAT (losing precision) and then to the type you want. You could also use the CAST keyword (MySQL) or a :: cast (Postgres).

peufeu