In programming, data type is an important concept.
Variables can store data of different types, and different types can do different things.
RPG has the following data types built-in by default, in these categories:
Text: | char, varchar |
Numeric: | int, packed, zoned, uns |
Date and Time: | date, time, timestamp |
Binary: | bin |
Boolean | ind |
Pointer | pointer |
SQL Types: | clob, blob |
The different types
Data Type |
Description |
Example |
char | Character. A string with a defined length. |
dcl-s FirstName char(128); |
varchar | Variable Character. A string with a variable length with the max length defined. Note that the field internally has two bytes that hold the actual used length of the field. |
dcl-s FirstName varchar(128); |
int | Integer. A numerical integer field that stores binary and that has got a set of predefined types with limits of how big numbers it can hold. Also stores the sign of the number, i.e if it’s positive or negative. |
// Value -128 to 127 (1 byte) |
packed | Packed. A numeric datatype that can hold decimal numbers. Stores two digits per byte and the max size needs to be odd in order to hold the positive/negative sign. |
// A Packed Field with a maximum of 7 numbers, where two are decimal positions. Max value is 99999.99. Occupies only 4 bytes // A packed field without decimals. Max value is 9999999 |
zoned | Zoned. A numeric datatype that can hold decimal numbers. Stores one digit per byte and needs to be defined with a maximum number of digits to hold. |
// A zoned field with a maximum of 7 numbers, where two are decimal positions. Max value is 99999.99. Occupies only 7 bytes // A packed field without decimals. Max value is 9999999 |
uns | Unsigned. A numerical integer field that stores binary and that has got a set of predefined types with limits of how big numbers it can hold. Does NOT store the sign of the number, i.e if it’s positive or negative. Only positive numbers are allowed. |
// Value 0 to 255 (1 byte) dcl-s unsignedInteger3 int(3) // Value 0 to 65,535 (2 bytes) dcl-s unsignedInteger5 int(5) // Value 0 to 4,294,967,295 (4 bytes) dcl-s unsignedInteger10 int(10); // Value 0 to 18,446,744,073,709,551,615 (8 bytes) dcl-s unsignedInteger20 int(20); |
date | Date. A datatype that contains proper dates. Like a built in calendar. Stores only the date with the default format being *ISO (YYYY-MM-DD). There are many different formats with different separators to choose from. Can not store invalid dates like 2020-07-45… |
// A date field. Will use the default format // A date field given a 2digit year format. |
time | Time. A datatype that stores time in a proper format. Can be used to perform time calculations. There are many different formats with different separators but the default being *ISO (hh.mm.ss) |
// A time field. Will use the default format // A time field with different separator |
timestamp | Timestamp. A complete date and time combined down to 12 fractions of a second. Kind of like a stamp in time, an exact moment in time. The milliseconds are optional but defaults to a precision of 6 digits. (YYYY-MM-DD-hh:mm:ss.ffffff) |
// A default timestamp (YYYY-MM-DD-hh:mm:ss.ffffff) // A timestamp without fractional seconds (YYYY-MM-DD-hh:mm:ss) // A timestamp with maximum fractional seconds (YYYY-MM-DD-hh:mm:ss.ffffffffffff) |
bin | Binary. A binary data type called binary decimal. It’s binary like int and uns but is NOT recommended to use any more thus it has built in limitations. It has a heritage in how the implementation was done back in the days. Exists mainly for compatibility reasons. |
// A binary decimal variable |
ind | Indicator. RPG’s equivalent of a boolean. It can only hold the values ‘0’ and ‘1’, indicating if something has occurred or not.It’s either on or off. True or false. |
// An indicator with its default value (*off = ‘0’) // An indicator initilized to be *on = ‘1’ |
pointer | Pointer.
Basing pointers are used to locate the storage for based variables. It’s like pointing to a place in memory, able to pass that reference around. A little bit more hardcore programming. |
dcl-s aPointer pointer; |
clob | Character Large Object. It’s a data type that can store large amounts of character data, around 2 GB. Used for large text documents. |
// A CLOB. Needs to be defined with a max size dcl-s aCLOB sqltype(CLOB:500) |
blob | Binary Large Object. It’s a data type that can store binary data but differs from other binary data types, like int, in that it is typically used to store images, audio files, videos etc. |
// A BLOB. Needs to be defined with a max size |