3 popular Ways to Handle DB2 VARCHAR Field in COBOL

Here’re the methods to handle DB2 VARCHAR data in COBOL. These are helpful for DB2-COBOL developers. In COBOL, you need 49 levels to receive VARCHAR data. Below are the methods you need to handle padded spaces.

COBOL 49 Level for VARCHAR

COBOL Logic 49 level

01 SNAME.   
   49 SNAME-LENGTH  PIC S9(4) COMP.   
   49 SNAME-TEXT    PIC X(100). 

SNAME-LENGTH contains the length of SNAME-TEXT. You can see this when displaying the SNAME field.

When the data in SNAME_TEXT is less than 100 bytes trailing spaces will be added. To use the SNAME_TEXT field in your COBOL program, first, you need to remove padded spaces.

Explanation

SNAME-TEXT should be the same length as that of Table column size (VARCHAR(100)).

Before moving data to SNAME-TEXT, you need to move spaces to it. Otherwise, junk data mixed up with actual value.

For exampleIf the SNAME column contains Blanks, the SNAME-LENGTH will be 95, the first five bytes of SNAME-TEXT will contain Blank, and the remaining 95 bytes will have the same data before the moving column to the host variable.

How to Write COBOL to DB2

To insert/update a record from COBOL 49 level to DB2, you need to take care of padded blanks. Else, these padded Blanks will pass to the VARCHAR column of the DB2 Table.

In COBOL, for instance, you try to move data to the Alpha-Numeric field, the PIC(X(n)) field padded with spaces when data length is less than the field size.

Get new content delivered directly to your inbox.

3 Methods to Remove Padded Spaces

Remove Padded spaces – Method-1

Assign each character of the value to an array, count the number of trailing blanks, and subtract the value from the total length of the string.

Remove Padded spaces – Method-2

Use UNSTRING to get Length. This logic gives actual string length value .

UNSTRING SNAME-TEXT 
DELIMITED BY ' ' 
COUNT IN SNAME-LENGTH.

Remove Padded spaces – Method -3

POSSTR scalar function can be used to determine the position in a string of another string:

SQL Query with POSSTR

EXEC SQL 
SET :SNAME-LENGTH = POSSTR('ERIC CLARK, ', ') - 1
END-EXEC.

:SNAME-LENGTH contains 10 (there are 10 bytes in ERIC CLARK before trailing blanks)

The Bottom Line

Be mindful while using UNSTRING and POSSTR techniques. It ignores blanks character string. For example, in the ‘EricbbClark’, you’ll lose the ‘Clark.’

Related Posts


Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.

Comments are closed.