Db2 SQLCODE

SQLCODE -604: Invalid length, precision or scale

SQLCODE -604 (SQLSTATE 42611) means a data type in a CREATE or ALTER statement has a length, precision or scale that Db2 doesn't allow, such as DECIMAL(38,2), DECIMAL(5,7) or CHAR(300).

Other names for this code

Also written as SQL -604, SQLCODE=-604, SQLCODE = -604, db2 -604, sqlstate 42611, SQL0604N, invalid length, precision, or scale attribute.

What it means

Each Db2 data type has limits on its length, precision and scale. When a column, parameter or other data type definition goes past those limits, or is malformed, Db2 rejects the whole statement with -604. Nothing is created or altered.

The limits people hit most on Db2 for z/OS are these.

  • DECIMAL precision runs from 1 to 31, and the scale can't be larger than the precision.
  • CHAR is 1 to 255 bytes. Longer fixed text needs VARCHAR.
  • VARCHAR and VARBINARY go up to 32704 bytes. Beyond that you need a CLOB or BLOB.
  • GRAPHIC is 1 to 127 characters.
  • FLOAT(n) takes n from 1 to 53.
  • TIMESTAMP(p) takes a precision from 0 to 12.

A CREATE TABLE LIKE that copies a view can also get -604 when a column of the view has a length that isn't valid for a table column.

Common causes

  • DDL converted from another database carries over a type Db2 can't hold, such as NUMBER(38) from Oracle becoming DECIMAL(38), or DECIMAL(38) from SQL Server.
  • A CHAR column longer than 255 bytes, often from a design that used fixed-length text everywhere.
  • A typo that puts the scale above the precision, such as DECIMAL(5,7), or a length of zero.
  • A TIMESTAMP precision above 12.
  • A modeling tool generated the DDL for a different target database.
  • CREATE TABLE LIKE a view whose column has a length a table can't use.

How to find the problem

  1. Read every data type in the failing statement and compare it with the limits above. The z/OS message doesn't always point at the column, so check each one.
  2. For converted DDL, search for DECIMAL and NUMERIC with precision above 31, and CHAR above 255.
  3. For CREATE TABLE LIKE a view, check the view's column lengths in SYSIBM.SYSCOLUMNS.

How to fix it

  • Bring DECIMAL down to a precision of 31 or less. If the values really need more digits, consider DECFLOAT(34).
  • Change CHAR longer than 255 to VARCHAR, and VARCHAR longer than 32704 to CLOB.
  • Fix any scale larger than its precision.
  • Set your modeling or conversion tool to generate DDL for Db2 for z/OS, then review the numeric and character types by hand.
  • Remember that a long VARCHAR also has to fit the table space page size. A row that is too long gets its own error, -670.

IBM documentation

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

Report a correction to WND Software.

Questions

Why can't I use DECIMAL(38) like in Oracle or SQL Server?

Db2 caps DECIMAL precision at 31 digits. Use DECIMAL(31,s) if the values fit, or DECFLOAT(34) for larger numbers.

Is -604 the same on Db2 for LUW?

Yes. Db2 for LUW reports SQL0604N with SQLSTATE 42611 and names the data item. The limits differ a little. LUW caps CHAR at 254 bytes, for example, while z/OS allows 255.

My VARCHAR is within 32704 but CREATE TABLE still fails. Why?

The row has to fit the table space page. If the total row length is too long for the page size, Db2 returns -670, not -604.