Skip to content
Advertisement

Error Code: 1265. Data truncated for column

I have a table named pt_products with the following fields

I create a new table

Then, I want to insert fields from the table pt_products to the graph table created above

And that’s when I get the error

Price field is

and avg_price from the destination table graph is the same.

Shouldn’t the data fit there?

I tried to change avg_price from graph table to decimal(20,4), with no luck.

I really can’t see what is going on.

example tests here

Thanks

Advertisement

Answer

The AVG() function on a NUMERIC(10,2) column like your price column produces a result with the type NUMERIC(14,6).

Demo:

(NUMERIC and DECIMAL are synonyms.)

When you try to stuff values with six digits of scale into a column with two digits of scale, MySQL must assume that it’s possible that it will lose some information along the way.

Demo: Storing a number with two digits of scale is okay, and causes no warnings:

But a number with greater scale results in a warning:

You can explicitly cast the numeric expression to the same data type, to avoid the warning:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement