How to check for Null Indicator in COBOL?

RDBMS from IBM and IBM's Hierarchical DBMS and Transaction Manager (IMS DC).
Post Reply
Mukul Roy
Website Team
Website Team
Posts: 24
Joined: Wed Jul 17, 2013 9:07 am

How to check for Null Indicator in COBOL?

Post by Mukul Roy »

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.
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1895
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: How to check for Null Indicator in COBOL?

Post by Robert Sample »

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
Global Moderator
Posts: 826
Joined: Wed Sep 11, 2013 3:57 pm

Re: How to check for Null Indicator in COBOL?

Post by enrico-sorichetti »

googling with
cobol sql null column processing examples
will return many many links with the relevant code snippets
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 8-)
Mukul Roy
Website Team
Website Team
Posts: 24
Joined: Wed Jul 17, 2013 9:07 am

Re: How to check for Null Indicator in COBOL?

Post by Mukul Roy »

Thanks Enrico, I thought I'll get a better understanding here..
User avatar
Anuj Dhawan
Founder
Posts: 2802
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Re: How to check for Null Indicator in COBOL?

Post by Anuj Dhawan »

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:

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');
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):

Code: Select all

05  COL_B-NULL-INDICATOR    PIC S9(4)   COMP.
05  COL_C-NULL-INDICATOR    PIC S9(4)   COMP.
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:

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.
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:

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
Hope this helps.
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.
Mukul Roy
Website Team
Website Team
Posts: 24
Joined: Wed Jul 17, 2013 9:07 am

Re: How to check for Null Indicator in COBOL?

Post by Mukul Roy »

Greatly explained. This helps. Thank you.
Post Reply

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

Register

Sign in

Return to “IBM DB2 and IMS DB/DC”