Search code examples
db2cobol

Definition of COBOL variable to use for DB2 date arithmetic


I have cases where I want to a add or substract a variable number of days from a timestamp.

The most simple example is this:

SELECT CURRENT_TIMESTAMP - :MOD-DAY DAY
INTO :MYTIMESTAMP        
FROM SYSIBM.SYSDUMMY1                  

My problem is figuring out the right cobol definition for MOD-DAY.
As far as I'm aware, we are running DB2 version 11.

According to https://www.ibm.com/support/knowledgecenter/SSEPEK_11.0.0/sqlref/src/tpc/db2z_datearithmetic.html
the DB2 definition of the variable must be DECIMAL(8,0)

That could be 9(08) or S9(08) but in both cases, and any other variation I have thought up so far, I get the compile error DSNH312I E DSNHSMUD LINE 1181 COL 49 UNDEFINED OR UNUSABLE HOST VARIABLE "MOD-DAY"

I have of course made certain that MOD-DAY has been defined, so the key word must be UNUSABLE

The error code definition for DSNH312I is pretty generic:
https://www.ibm.com/support/knowledgecenter/SSEPEK_10.0.0/msgs/src/tpc/dsnh312i.html

So does anyone know the right COBOL variable definition to use in this case?


Solution

  • Decimal in Mainframe-DB2 means comp-3.

    So the field should be defined as S9(08) comp-3


    If you look at the Cobol Copybooks generated by DB2 for DB2 Tables / Views you will see both the DB2 definition and the generated Cobol fields. That can be another way to solve queries like this