Db2 SQLCODE

SQLCODE -408: Value not compatible with target type

SQLCODE -408 (SQLSTATE 42821) means a statement tries to assign a value to a column or variable whose data type can't accept it, such as a number into a DATE column. The message names the target.

Other names for this code

Also written as SQL -408, SQLCODE=-408, SQLCODE = -408, db2 -408, sqlstate 42821, SQL0408N, db2 value not compatible with data type, SQL0408, value is not compatible with the data type of its target.

What it means

Db2 checks types when it assigns a value, in an INSERT, an UPDATE SET, a MERGE, a SET statement or a VALUES INTO. The source and target must be compatible. Numbers can go into numeric or character targets. Strings go into string targets. Date and time targets take date and time values or strings that hold valid ones. When the types can't be matched, Db2 rejects the statement with -408, usually when it is bound or prepared.

-408 is about putting a value somewhere. The same kind of mismatch inside a comparison, in a WHERE clause for example, is -401 instead.

Db2 10 for z/OS began casting between strings and numbers implicitly, so some assignments that used to fail now work. Mixing numbers with dates, binary data with text, or distinct types with their source types still needs an explicit conversion.

Common causes

  • A number is assigned to a DATE, TIME or TIMESTAMP column, often a YYYYMMDD value held in a numeric field.
  • A date or timestamp is assigned to a numeric column.
  • A binary string (BINARY, VARBINARY or BLOB) is assigned to an ordinary character column, or the other way round.
  • A value of a distinct (user-defined) type is assigned without a cast, or a plain value is assigned to a distinct-type column.
  • An XML value is assigned to a character column without XMLSERIALIZE.
  • In a native SQL procedure or function, a SET or RETURN gives a variable a value of the wrong type.
  • A result set locator variable holds a value that didn't come from ASSOCIATE LOCATORS.

How to find the problem

  1. Take the target name from the message and look up its type in SYSIBM.SYSCOLUMNS, or in the routine's parameters in SYSIBM.SYSPARMS.
  2. Work out the type of the source expression. For a host variable, check its declaration against DCLGEN output for the table.
  3. For distinct types, check SYSIBM.SYSDATATYPES to see the source type.

How to fix it

  • Convert explicitly. Use DATE or TIMESTAMP_FORMAT to turn strings into dates, CHAR or VARCHAR_FORMAT to turn dates into strings, and CAST for most other pairs.
  • For a YYYYMMDD number, the simplest fix is in the program. Build a date string such as 2026-10-18 in a PIC X(10) field and pass that instead.
  • Use XMLSERIALIZE to turn XML into text, and the cast functions Db2 created for distinct types.
  • Declare host variables to match the column. DCLGEN avoids most of these mismatches.

IBM documentation

IBM documentation: -408. Use the documentation for your installed release.

Report a correction to WND Software.

Questions

What is the difference between -408 and -401?

-408 is a type mismatch when a value is assigned to a column or variable. -401 is the same kind of mismatch inside a comparison or arithmetic.

Why do I get -180 instead of -408 for a date?

-180 means the types are fine, since a string can go into a date column, but the string's contents aren't a valid date format. -408 means the type itself can't be assigned.

Is SQL0408N on Db2 for LUW the same?

Yes. Db2 for LUW reports SQL0408N with SQLSTATE 42821, and its message also names the target.