In IBM DB2, the CHAR data type is used to store fixed-length character strings, meaning the database always allocates the full defined storage length regardless of the actual data size. As a result, CHAR columns can waste disk space when the stored values do not fully utilize the assigned length. This makes CHAR suitable only when the data length is consistent across all records.
In contrast, the VARCHAR data type in DB2 stores variable-length character data, using only the space required for the actual content. VARCHAR includes a two-byte length prefix that records the number of characters stored. For example, employee surnames within a department often vary in length, making VARCHAR the ideal choice for efficient database storage. However, a key limitation of VARCHAR in DB2 is that the column value must fit entirely within a single database page, so VARCHAR should be selected when data size varies significantly but remains within page limits.
When a DB2 table column is defined as VARCHAR, such as a DESCRIPTION column, the corresponding COBOL host variable mapping requires two fields. One variable (for example, DESCRIPTION-LEN) stores the length of the data, while the second variable (for example, DESCRIPTION-TEXT) contains the actual character data. This structure ensures accurate data handling between DB2 and COBOL applications.

Leave a comment