An Introduction to Variable Field Lengths in RPG
RPG experts Jon Paris and Susan Gantner explain new capabilities available with the Fall 2019 IBM i TR
We first wrote about variable length fields back in 2002. We decided this is a good time to revisit this topic for a number of reasons:
- As more and more people make use of SQL to define their tables, we see a corresponding increase in the usage of variable length fields. If those tables are to be used with native I/O operations, RPGers need to understand how to deal with them.
- We’ve recently seen a number of programmers moving to IBM i from other platforms, and for them, variable length fields tend to be the norm and so naturally, they use them on IBM i as well.
- Variable length fields offer many benefits to the RPG programmer as they can simplify and clarify logic, improve efficiency and just make things “better” all around.
- Last, but by no means least, when we reviewed our original article we found that it was all fixed-format RPG—including the calculations! *Shudder!*
Defining Variable Length Fields in RPG
Let’s begin at the beginning. Variable-length fields can be defined either internally within your RPG program, or on the database. We’ll get to the database aspects later; for now we’ll just look at defining and using variable length fields within RPG.
Definition within a program is a simple matter. The first example shows the old D-spec style definition where the keyword VARYING is used. In the new free-format (and all of our subsequent examples will use that format) you simply specify Varchar as the data type. These two field definitions accomplish basically the same thing: