---
title: "3 Popular Ways to Handle DB2 VARCHAR Field in COBOL"
description: "Handling VARCHAR in COBOL is a tricky subject. Explained to handle it in COBOL 49 level with examples in a way that a novice can understand quickly."
url: https://srinimf.com/2014/08/14/3-ways-to-handle-db2-varchar-filed-in-cobol/
date: 2014-08-14
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2014/08/pexels-photo-5676744.jpeg?fit=1880%2C1253&ssl=1
categories: ["DB2"]
tags: ["Db2 Varchar"]
type: post
lang: en
---

# 3 Popular Ways to Handle DB2 VARCHAR Field in COBOL

Here’re three methods to handle DB2’s VARCHAR data in COBOL. These are helpful for DB2-COBOL developers. In COBOL, you need level 49 to receive VARCHAR data. The below methods explain [how to](https://srinimf.com/2018/09/13/rpg-programming-language-unique-concepts-to-learn-quickly/) handle VARCHAR data.

Related[COBOL: Assumed Decimal Vs Dot](https://srinimf.com/2018/07/27/cobol-real-answer-on-assumed-decimal-vs-dot/)

## 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 must 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 must move spaces to it. Otherwise, junk data is mixed up with actual value.

For instance**, **If 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](https://srinimf.com/2012/11/13/cics-coding-for-screen-navigation/) COBOL to DB2

To insert/update a record from COBOL 49 level to DB2, you must 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.

## How to remove padded spaces: Here’re 3 methods

### 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

Use scalar function (POSSTR) 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.
```

The host-variable SNAME-LENGTH contains 10 (since there are 10 bytes in ERIC CLARK before trailing blanks)

**The Bottom Line**

While using UNSTRING and POSSTR functions since they ignore blank character-string. For instance, in the ‘Eric*bb*Clark’, you’ll lose the ‘Clark.’

[**32 Complex SQL Queries**](https://srinimf.com/e-books/)

Related Posts

- [How to Write COBOL Logic for DB2 VARCHAR Field](https://srinimf.com/2015/08/15/all-about-mainframe-db2-varchar-field/)DateAugust 15, 2015
- [How to Create SQL UDF Quickly](https://srinimf.com/2017/10/12/spilt-string-sql-logic-to-create-function/)DateOctober 12, 2017
- [Logic to call COBOL program from stored Procedure](https://srinimf.com/2013/03/22/db2-calling-cobolcjava-programs-from-stored-proc/)DateMarch 22, 2013

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2014%2F08%2F14%2F3-ways-to-handle-db2-varchar-filed-in-cobol%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2014%2F08%2Fpexels-photo-5676744.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=3%20Popular%20Ways%20to%20Handle%20DB2%20VARCHAR%20Field%20in%20COBOL)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2014/08/14/3-ways-to-handle-db2-varchar-filed-in-cobol/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2014/08/14/3-ways-to-handle-db2-varchar-filed-in-cobol/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2014/08/14/3-ways-to-handle-db2-varchar-filed-in-cobol/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2014/08/14/3-ways-to-handle-db2-varchar-filed-in-cobol/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%203%20Popular%20Ways%20to%20Handle%20DB2%20VARCHAR%20Field%20in%20COBOL&body=https%3A%2F%2Fsrinimf.com%2F2014%2F08%2F14%2F3-ways-to-handle-db2-varchar-filed-in-cobol%2F&share=email)
-

### Like this:

Like Loading…
