SQLCODE -413: Overflow during numeric conversion
SQLCODE -413 (SQLSTATE 22003) means an overflow or underflow happened while Db2 converted a number from one numeric data type to another, for example a large DECIMAL value cast to SMALLINT. The statement fails and nothing is changed.
Other names for this code
Also written as SQL -413, SQLCODE=-413, SQLCODE = -413, db2 -413, sqlstate 22003, SQL0413N, db2 overflow numeric conversion, SQL0413, overflow or underflow occurred during numeric data type conversion.
What it means
Db2 converts numbers between types all the time, in an explicit CAST, in functions such as SMALLINT, INTEGER, BIGINT and DECIMAL, and when it brings values of different types together. Each target type has a range. When the value being converted is too big for the target, or too small for a floating-point target to hold, Db2 can't represent it and stops the statement with -413.
Several codes describe numbers that don't fit, and the right one tells you where to look. -413 is a conversion inside SQL. -406 is a calculated value stored in a column too small for it. -802 is an overflow in the arithmetic itself, such as a multiplication or a SUM. -304 is a value returned to a host variable that is too small.
Common causes
- A CAST or conversion function narrows a value that is too large, for example
SMALLINT(QTY)when some rows hold more than 32,767, orINTEGER(TOTAL)on a BIGINT total. DECIMAL(expr, 5, 2)orCAST(expr AS DECIMAL(5,2))is applied to values of 1,000 or more.- A floating-point or DECFLOAT value far outside the DECIMAL range is converted to DECIMAL.
- Data has grown past what a conversion in an old query assumed, for example IDs that used to fit in INTEGER.
- A bad row, such as a sentinel value of all nines, slips into data that is normally small.
How to find the problem
- List every explicit CAST and conversion function in the statement, and note the target type of each.
- Check the range of the source data with
SELECT MIN(QTY), MAX(QTY) FROM ...using the same WHERE clause. - Find the rows that break the limit, for example
SELECT ORDER_.NO, QTY FROM ... WHERE ABS(QTY) > 32767 - If the statement has no explicit conversion, look at expressions that mix numeric types and at the types of host variables passed in.
How to fix it
- Convert to a type big enough for the data, such as INTEGER or BIGINT instead of SMALLINT, or a DECIMAL with more digits.
- Guard the conversion with a CASE expression that handles out-of-range values in the way the business wants.
- Fix bad rows at the source if the large values shouldn't exist.
- Review older queries and copybooks that assume small values whenever a key or counter grows.
IBM documentation
IBM documentation: -413. Use the documentation for your installed release.
Related codes
Questions
What is the difference between -413 and -406?
-413 is an overflow while converting between numeric types inside the SQL. -406 is a calculated value that doesn't fit the column it's being stored in.
Can -413 come from a FETCH into a host variable that is too small?
That case is normally reported as -304, or +304 with the indicator set to -2 when the program supplied an indicator variable.
Is it the same on Db2 for LUW?
Yes. Db2 for LUW reports SQL0413N with SQLSTATE 22003 for an overflow during numeric data type conversion.