For each column that allows nulls, DB2 allocates one extra byte of storage for an indicator.
In programming languages that do not use nulls, like COBOL or C, a host indicator variable is used together with a host variable to indicate when a column is null on retrieval, or when a column is to be set to null on update.
A negative number indicates the column is null, usually -1
A –2 indicates a data conversion error
A positive number or zero indicates the column is not null
If a column is defined as a CHAR data type, and if the value has been truncated on retrieval because the host variable is not large enough, the indicator variable contains the original length of the truncated column
Defining Null Indicators
05 IND-NULL PIC S9(4) COMP VALUE -1.
05 IND-VALUE PIC S9(4) COMP VALUE 0.
05 IND-APP-DATE PIC S9(4) COMP.
05 IND-BIRTH-DATE PIC S9(4) COMP.
05 IND-TCHIS-DATE PIC S9(4) COMP.
05 IND-TCHEX-DATE PIC S9(4) COMP.
05 IND-SUBIS-DATE PIC S9(4) COMP.
05 IND-SUBEX-DATE PIC S9(4) COMP.
This program has the two constants, –1 and 0, clearly defined in working storage with descriptive names IND-NULL and IND-VALUE.
Indicators are defined in COBOL as PIC S9(04) COMP.
If a null value is retrieved and no null indicator is used, a –305 SQLCODE is returned.
Selecting with Null Indicators
The indicator appears immediately after its corresponding host variable, with no comma in between.
Checking the Null Indicator on Select
IF IND-TCHIS-DATE = IND-VALUE
MOVE SUB-LIC-ISSUE-DATE TO WS-DB2-DATE
MOVE WS-D-MM TO HA2TLIMMO
MOVE WS-D-DD TO HA2TLIDDO
MOVE WS-D-YY TO HA2TLIYYO
MOVE ALL '_' TO HA2TLIMMO
If the indicator = 0 (IND-VALUE), a valid date has been returned into the host variable (SUB-LIC-ISSUE-DATE).
If the indicator = -1 (ELSE in the code above), the date was null in the database, and the value in the host variable is not used.
(Beware! Checking for a valid date in the host variable is not sufficient to detect a null! In CICS, the host variable may be initialized, but in a batch program processing a cursor, the value in the host variable is likely to be the last non-null value retrieved.)
Be Prepared for Unexpected Nulls
In some cases, even when you haven’t defined the column to be NULL, a SQL query might return a NULL. Some reasons for unexpected nulls in query results include:
A missing row in an outer join
A data exception
No row found for a selected columnar function
A subselect of a columnar function when no row is found
Setting the Null Indicator for Insert or Update
IF HA2TLIMMI NOT = ALL '_'
MOVE HA2TLIMMI TO WS-D-MM
MOVE HA2TLIDDI TO WS-D-DD
MOVE HA2TLIYYI TO WS-D-YY
MOVE WS-DB2-DATE TO SUB-LIC-ISSUE-DATE
MOVE IND-VALUE TO IND-TCHIS-DATE
MOVE IND-NULL TO IND-TCHIS-DATE.
SET ST_AT_SCHOOL_NO = :SUB-ST-AT-SCHOOL-NO,
ST_AT_SCHOOL = :SUB-ST-AT-SCHOOL,
LIC_EXPIRE_DATE = :SUB-LIC-EXPIRE-DATE
LIC_ISSUE_DATE = :SUB-LIC-ISSUE-DATE
UPDTTIMESTAMP = CURRENT TIMESTAMP
If a valid value is in the host variable, the indicator is set to zero.
If not, the indicator is set to –1 and the host variable is not used.
Nice Date Validation Routine
03 WS-DTEE-YY PIC X(4).
03 WS-DTEE-MM PIC X(2).
03 WS-DTEE-DD PIC X(2).
03 WS-S-MM PIC X(2).
03 F PIC X(1) VALUE '/'.
03 WS-S-DD PIC X(2).
03 F PIC X(1) VALUE '/'.
03 WS-S-YEAR PIC X(4).
01 VALID-DB2-DATE PIC X(10) VALUE SPACES.
MOVE WS-Y TO WS-DATE-SW.
IF WS-DATE-EDIT NUMERIC AND
WS-DTEE-YY > '1900'
MOVE WS-N TO WS-DATE-SW
GO TO 00610-EXIT.
MOVE WS-DTEE-DD TO WS-S-DD.
MOVE WS-DTEE-MM TO WS-S-MM.
MOVE WS-DTEE-YY TO WS-S-YEAR.
MOVE WS-WORK-DATE TO VALID-DB2-DATE.
IF SQLCODE NOT = 0
MOVE WS-N TO WS-DATE-SW.
The concept of a null value is used in DB2 to represent data that is unknown or not applicable
Nulls cause a third possibility for comparisons – true, false, or null
At creation, columns must be defined as null, not null, or not null with default
Null indicators are used to convey information about null columns to programming languages without the concept of null
Be alert for the possibility of null being returned from a query, even when the columns you are working with are not null.