A DB2 null Indicator/value represents missing or unknown information at the column level. When a column is set as null, it can mean one of two things: the attribute is not applicable for certain occurrences of the entity, or the attribute applies to all entity occurrences, but the information may not always be known. It could be a combination of these two situations too. A DB2 null Indicator/value is not the same as Zero or blank. For all Columns that can contain Nulls, an indicator variable should be used in all Select Statements to retrieve data -or- the COALESCE built-in function should be used.
When working with COBOL programs and DB2 databases, it is important to validate that fields in the database do not contain null values. This is because COBOL programs may not be able to handle null values and may not function properly if they encounter them.
INTO :HOSTVAR:NULLIND
The picture clause that should be used for the DB2 null indicator is S9(4) COMP
Meaning:
-1: the field is null
0: the field is not null
-2: the field value is truncated
Inserting a record with a nullable column
• To insert a NULL, move -1 to the null indicator
• To insert a valid value, move 0 to the null indicator
Every column defined in a DB2 table must be designated as either allowing or disallowing nulls. A column is defined as nullable – meaning it can be set to NULL – in the table creation DDL. Null is the default if nothing is specified after the column name. To prohibit the column from being set to NULL you must explicitly specify NOT NULL after the column name. In the following sample table, COL1 and COL3 can be set to null, but not COL2, COL4, or COL5.
Table Declaration with NULL
CREATE TABLE SAMPLE (COL1 INTEGER, COL2 CHAR(10) NOT NULL, COL3 CHAR(5), COL4 DATE NOT NULL WITH DEFAULT, COL5 TIME NOT NULL);
Select Query with NULL
EXEC SQL SELECT TQ_UOWPRR_DESC ,TQ_UOWPRIOR_ID ,TQ_PRRACTVTY_DT ,TQ_MIGRATION_DT ,TQ_DELETION_IND INTO TQ_UOWPRR_DESC :IND-UOWPRR-DESC ,TQ_UOWPRIOR_ID :IND-UOWPRIOR-ID ,TQ_PRRACTVTY_DT :IND-PRRACTVTY-DT ,TQ_MIGRATION_DT :IND-MIGRATION-DT ,TQ_DELETION_IND :IND-DELETION1IND FROM TQ1 WHERE TSQRY_PRODSGMT_CD = :TSQRY-PRODSGMT-CD AND TSQRY_CLNTSGMT_CD = :TSQRY-CLNTSGMT-CD AND TQ_REC_ID = :TQ-REC-ID AND TQ_TPPREFIX_CD = :TQ-TPPREFIX-CD END-EXEC.
Declare Cursor with NULL
EXEC SQL DECLARE TQ_CSR CURSOR FOR SELECT TQ_UOWPRR_DESC ,TQ_UOWPRIOR_ID ,TQ_PRRACTVTY_DT ,TQ_MIGRATION_DT ,TQ_DELETION_IND FROM TQ1 WHERE TSQRY_PRODSGMT_CD = :TSQRY-PRODSGMT-CD AND TSQRY_CLNTSGMT_CD = :TSQRY-CLNTSGMT-CD AND TQ_REC_ID = :TQ-REC-ID AND TQ_TPPREFIX_CD = :TQ-TPPREFIX-CD END-EXEC.
Fetch Cursor with NULL
EXEC SQL FETCH TQ_CSR INTO :TQ-UOWPRR-DESC:IND-UOWPRR-DESC, :TQ-UOWPRIOR-ID:IND-UOWPRIOR-ID, :TQ-PRRACTVTY-DT:IND-PRRACTVTY-DT, :TQ-MIGRATION-DT:IND-MIGRATION-DT, :TQ-DELETION-IND:IND-DELETION-IND END-EXEC.
Rowset with NULL
EXEC SQL FETCH NEXT TQ_CSR FOR 10 ROWS INTO :TQ-UOWPRR-DESC:IND-UOWPRR-DESC, :TQ-UOWPRIOR-ID:IND-UOWPRIOR-ID, :TQ-PRRACTVTY-DT:IND-PRRACTVTY-DT, :TQ-MIGRATION-DT:IND-MIGRATION-DT, :TQ-DELETION-IND:IND-DELETION-IND END-EXEC.
The purposes of the indicator variable are to
For example, if :TQ-UOWPRR-DESC: IND-UOWPRR-DESC is used to specify an insert or update value, and if IND-UOWPRR-DESC is negative, the value is specified in the null value. If IND-UOWPRR-DESC is not negative the value specified in the value of TQ-UOWPRR-DESC.
Similarly, if TQ-UOWPRR-DESC: IND-UOWPRR-DESC is specified in a VALUES INTO clause or in a FETCH or SELECT INTO statement, and if the value returned is null, TQ-UOWPRR-DESC is not changed and IND-UOWPRR-DESC is set to a negative value. If the value returned is not null, that value is assigned to TQ-UOWPRR-DESC, and IND-UOWPRR-DESC is set to zero (unless the assignment to TQ-UOWPRR-DESC requires string truncation of a non-LOB string; in which case IND-UOWPRR-DESC is set to the original length of the string). If an assignment requires truncation of the seconds part of the time, IND-UOWPRR-DESC is set to the number of seconds.
SQL Code -305
If the table column is defined as NOT NULL (with no default) and if we try to insert a null value we get this error.
Resolution: This should be resolved by making sure that the inserted value is not null. DB2 Null indicator cannot be used here since the column is defined as NOT NULL.
(validate the data, if it’s not numeric or less than spaces then move spaces into it and then insert or update into the table)
A table column is defined as NULL, The host variable has a not null value and the DB2 Null indicator is not set in the host program, so the null indicator defaults to a negative value.
Resolution: This should be resolved by using a null indicator in the host program and moving the relevant value to the null indicator. Here in order to move the null value into the respective column move -1 to the null indicator.
When working with COBOL programs and DB2 databases, it is important to validate that fields in the database do not contain null values. This is because COBOL programs may not be able to handle null values and may not function properly if they encounter them. In this blog post, we’ll take a look at how to perform null value validation using COBOL and DB2.
To validate null values using COBOL and DB2, you can follow these steps:
Declare a NULLIND variable for each nullable field in the database. The NULLIND variable should be declared as S9(4) COMP.
01 CUSTOMER-NAME PIC X(30). 01 CUSTOMER-NAME-NULLIND S9(4) COMP.
In your COBOL program, retrieve the data from the database and check the value of the NULLIND variable for each field. If the value of the NULLIND variable is -1, the field contains a null value. If the value is 0, the field contains a valid value.
EXEC SQL SELECT CUSTOMER-NAME, CUSTOMER-NAME-NULLIND INTO :CUSTOMER-NAME, :CUSTOMER-NAME-NULLIND FROM CUSTOMER END-EXEC. IF CUSTOMER-NAME-NULLIND = -1 DISPLAY 'Error: Customer name is null.' ELSE DISPLAY 'Customer name is ' CUSTOMER-NAME END-IF.
If a null value is encountered, handle it appropriately in your COBOL program. This may involve setting a default value or taking other corrective action.
By validating null values using COBOL and DB2, you can ensure that your programs are processing only valid data and that they are not encountering unexpected errors due to null values. Additionally, by handling null values appropriately, you can improve the accuracy and reliability of your COBOL programs.
CASE Statement can be used to handle NULL values.
EMP_ID NAME EMAIL_ADDRESS 1 Jane Jane.D@outlook.com 1 John (null) 1 Sam sam.huges@yahoo.com
SELECT CASE WHEN EMAIL_ADDRESS IS NULL THEN 'Not specified' ELSE EMAIL_ADDRESS END AS EMAIL_ADDRESS FROM EMPLOYEE;
Result EMAIL_ADDRESS Jane.D@outlook.com Not specified sam.huges@yahoo.com
The COALESCE function returns the first non-NULL value it finds in the supplied parameters.
COALESCE (expression1, expression2, expression3…..)
Let’s take an example to find if any of the Personal, Work, or Alternative emails are present for an employee.
EMP_ID NAME WORK_EMAIL PERSONAL_EMAIL ALT_EMAIL 1 Jane Jane.D@outlook.com jane.doe@yahoo.com j.doe@gmail.com 1 John (null) john.kurian@gmail.com (null) 1 Sam (null) (null) sam.huges@yahoo.com
SELECT COALESCE(WORK_EMAIL, PERSONAL_EMAIL, ALT_EMAIL) FROM EMPLOYEE;
This query would return personal_email, but if that is NULL it would return work_email, and if that is NULL then alternative_email is returned.
Result Jane.D@outlook.com john.kurian@gmail.com sam.huges@yahoo.com
A well-maintained product backlog is crucial for successful product development. It serves as a single…
Incremental value to the customer refers to the gradual delivery of small, functional parts of…
A Product Market refers to the group of potential customers who might be interested in…
The Professional Agile Leadership - Evidence-Based Management (PAL-EBM) certification offered by Scrum.org is designed for…
The Professional Agile Leadership (PAL I) certification, offered by Scrum.org, is designed to equip leaders…
Choosing the right Scrum Master Certification depends on your current experience and career goals. If…