How to check for Null Indicator in COBOL?
How to check for Null Indicator in COBOL?
Hi,
When I select some data from DB2 using SQL in COBOL program and a column in DB2 contains Null then how to capture and test that the column is Null? If you can guide me, that will be helpful.
When I select some data from DB2 using SQL in COBOL program and a column in DB2 contains Null then how to capture and test that the column is Null? If you can guide me, that will be helpful.
- Robert Sample
- Global Moderator
- Posts: 1903
- Joined: Fri Jun 28, 2013 1:22 am
- Location: Dubuque Iowa
Re: How to check for Null Indicator in COBOL?
The method is described in the manual; IIRC you define the column with two 49-level variables and the first is set if the column is NULL and is not set if the column is not NULL while the second variable contains the value if the column is not NULL. Two variables must be used because otherwise there is no way to indicate that a value is NULL.
- enrico-sorichetti
- Global Moderator
- Posts: 843
- Joined: Wed Sep 11, 2013 3:57 pm
Re: How to check for Null Indicator in COBOL?
googling with
will return many many links with the relevant code snippetscobol sql null column processing examples
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort

Re: How to check for Null Indicator in COBOL?
Thanks Enrico, I thought I'll get a better understanding here..
- Anuj Dhawan
- Founder
- Posts: 2824
- Joined: Sun Apr 21, 2013 7:40 pm
- Location: Mumbai, India
- Contact:
Re: How to check for Null Indicator in COBOL?
I think I'll go in bit of explanation before answering your question -
SQL, unlike COBOL, supports variables that can contain null (values - the use of word "value" along with NULL is not usually encouraged as NULL itself means the absence of value and it in itself can not be termed as "value", well). Actually, a null "value" means that no entry has been made and usually implies that the value is either unknown or undefined, at the moment. For example, a null value in a joining date column does not mean that the date is undetermined, it means that the date is not known or has not been set yet.
For a column in a DB2 table for which null is allowed, special handling is required in a COBOL program. The program should define a null indicator variable for each column for which nulls are allowed. This null indicator variable should be defined as a binary halfword – PIC S9(4) COMP (why? - well, at the moment treat it as a rule) – and should be listed in the FETCH as an additional host variable immediately after the regular host variable. If the value of this indicator variable is less than zero then the attribute within the table was null.
To answer your question - let's begin with a CREATE table called MYTABLE which contains three fields: COL_A (which is NOT NULL), COL_B (nulls allowed) and COL_C (nulls allowed). We then use INSERT statements to populate the table:
Now below, the indicator variables are coded within the WORKING-STORAGE SECTION as follows (no such variable is coded for COL_A as it was defined as NOT NULL):
You can see that the null indicator variable is listed in the FETCH as an additional host variable immediately after the regular host variable as follows:
If the value retrieved from the table is null, the null indicator variable will contain a value less than zero, so we check the null indicator as follows:
Hope this helps.
SQL, unlike COBOL, supports variables that can contain null (values - the use of word "value" along with NULL is not usually encouraged as NULL itself means the absence of value and it in itself can not be termed as "value", well). Actually, a null "value" means that no entry has been made and usually implies that the value is either unknown or undefined, at the moment. For example, a null value in a joining date column does not mean that the date is undetermined, it means that the date is not known or has not been set yet.
For a column in a DB2 table for which null is allowed, special handling is required in a COBOL program. The program should define a null indicator variable for each column for which nulls are allowed. This null indicator variable should be defined as a binary halfword – PIC S9(4) COMP (why? - well, at the moment treat it as a rule) – and should be listed in the FETCH as an additional host variable immediately after the regular host variable. If the value of this indicator variable is less than zero then the attribute within the table was null.
To answer your question - let's begin with a CREATE table called MYTABLE which contains three fields: COL_A (which is NOT NULL), COL_B (nulls allowed) and COL_C (nulls allowed). We then use INSERT statements to populate the table:
Code: Select all
CREATE TABLE MYTABLE
( COL_A CHAR(2) NOT NULL,
COL_B CHAR(2) ,
COL_C CHAR(2) ) ;
Code: Select all
INSERT INTO MYTABLE VALUES ('A', 'B', 'C');
INSERT INTO MYTABLE VALUES ('D', 'E', NULL);
INSERT INTO MYTABLE VALUES ('F', NULL, G');
INSERT INTO MYTABLE VALUES ('H', NULL, NULL);
INSERT INTO MYTABLE VALUES ('I', 'J', 'K');
Code: Select all
05 COL_B-NULL-INDICATOR PIC S9(4) COMP.
05 COL_C-NULL-INDICATOR PIC S9(4) COMP.
Code: Select all
EXEC SQL
FETCH MYTABLE-CURSOR INTO
:MY-COL_A ,
:MY-COL_B :COL_B-NULL-INDICATOR,
:MY-COL_C :COL_C-NULL-INDICATOR
END-EXEC.
Code: Select all
IF COL_B-NULL-INDICATOR < 0
MOVE ALL '*' TO DL-COL_B
ELSE
MOVE MY-COL_B TO DL-COL_B
END-IF
Thanks,
Anuj
Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
Anuj
Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
Create an account or sign in to join the discussion
You need to be a member in order to post a reply
Create an account
Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute