Determine equivalent SQL and ILE RPG data types

The precompiler determines the base SQLTYPE and SQLLEN of host variables according to the following table. If a host variable appears with an indicator variable, the SQLTYPE is the base SQLTYPE plus one.

Table 1. ILE RPG declarations mapped to typical SQL data types
RPG data type RPG coding SQLTYPE of host variable SQLLEN of host variable SQL data type
Data structure (without subfields) Length = n where n ≤ 32766. 452 n CHAR(n)
Zoned data
  • Defined on Definition specification as subfield with data type S or blank.
  • Defined on Definition specification with data type S.
  • Defined on Input specification with data type S or blank.
488 p in byte 1, s in byte 2 NUMERIC(p, s) where p is the number of digits and s is the number of decimal places
Packed data
  • Defined on Definition specification with decimal positions (pos 69-70) not blank.
  • Defined on Definition specification subfield with data type P.
  • Defined on Definition specification with data type P or blank.
  • Defined on Input specification with data type P.
484 p in byte 1, s in byte 2 DECIMAL(p, s) where p is the number of digits and s is the number of decimal places
2-byte binary with zero decimal positions
  • Defined on Definition specification as subfield with from and to positions and data type B and byte length 2.
  • Defined on Definition specification with data type B and digits from 1 to 4.
  • Defined on Input specification with data type B and byte length 2
500 2 SMALLINT
4-byte binary with zero decimal positions
  • Defined on Definition specification as subfield with from and to positions and data type B and byte length 4.
  • Defined on Definition specification with data type B and digits from 5 to 9.
  • Defined on Input specification with data type B and byte length 4.
496 4 INTEGER
2-byte integer
  • Defined on Definition specification as subfield with from and to positions and data type I and byte length 2.
  • Defined on Definition specification with data type I and digits 5.
  • Defined on Input specification with data type I and byte length 2.
500 2 SMALLINT
4-byte integer
  • Defined on Definition specification as subfield with from and to positions and data type I and byte length 4.
  • Defined on Definition specification with data type I and digits 10.
  • Defined on Input specification with data type I and byte length 4.
496 4 INTEGER
8-byte integer
  • Defined on Definition specification as subfield with from and to positions and data type I and byte length 8.
  • Defined on Definition specification with data type I and digits 20.
  • Defined on Input specification with data type I and byte length 8.
492 8 BIGINT
short float Data type = F, length = 4. 480 4 FLOAT (single precision)
long float Data type = F, length = 8. 480 8 FLOAT (double precision)
Character Data type = A or blank, decimal positions blank, length between 1 and 32766. 452 n CHAR (n) where n is the length
Character varying length greater than 254 Data type = A or blank, decimal positions blank, VARYING keyword on Definition specification or format *VAR on Input specification. 448 n VARCHAR (n) where n is the length
Character varying length between 1 and 254 Data type = A or blank, decimal positions blank, VARYING keyword on Definition specification or format *VAR on Input specification. 456 n VARCHAR (n) where n is the length
graphic
  • Defined on Definition specification as subfield with from and to positions and data type G and byte-length b.
  • Defined on Definition specification with data type G and length n.
  • Defined on Input specification with data type G and byte-length b
468 m GRAPHIC(m) where m = n or m = b/2
varying graphic
  • Defined on Definition specification as subfield with from and to positions and data type G and byte-length b and VARYING keyword.
  • Defined on Definition specification with data type G and length n and VARYING keyword.
  • Defined on Input specification with data type G and byte-length b and format *VAR.
464 m VARGRAPHIC(m) where m = n or m = (b-2)/2
UCS-2
  • Defined on Definition specification as subfield with from and to positions and data type C and byte-length b.
  • Defined on Definition specification with data type C and length n.
  • Defined on Input specification with data type C and byte-length b.
468 m GRAPHIC(m) with CCSID 13488 where m = n or m = b/2
varying UCS-2
  • Defined on Definition specification as subfield with from and to positions and data type C and byte-length b and VARYING keyword.
  • Defined on Definition specification with data type C and length n and VARYING keyword.
  • Defined on Input specification with data type C and byte-length b and format *VAR.
464 m VARGRAPHIC(m) with CCSID 13488 where m = n or m = b/2
Date
  • Defined on Definition specification with data type D, format f and separator s from DATFMT keyword.
  • Defined on Input specification with data type D and format in pos 31-34, separator in pos 35.
384 n DATE DATFMT(f) DATSEP(s)1
Time
  • Defined on Definition specification with data type T, format f and separator s from TIMFMT keyword.
  • Defined on Input specification with data type T and format in pos 31-34, separator in pos 35.
388 n TIME TIMFMT(f) TIMSEP(s)1
Timestamp Data type Z. 392 n TIMESTAMP
1SQL creates the date/time subfield using the DATE/TIME format specified on the CRTSQLRPGI command. The conversion to the host variable DATE/TIME format occurs when the mapping is done between the host variables and the SQL-generated subfields.

The following table can be used to determine the RPG data type that is equivalent to a given SQL data type.

Table 2. SQL data types mapped to typical RPG declarations
SQL data type RPG data type Notes
SMALLINT Definition specification. I in position 40, length must be 5 and 0 in position 42.

OR

Definition specification. B in position 40, length must be ≤ 4 and 0 in position 42.

 
INTEGER Definition specification. I in position 40, length must be 10 and 0 in position 42.

OR

Definition specification. B in position 40, length must be ≤ 9 and ≥ 5 and 0 in position 42.

 
BIGINT Definition specification. I in position 40, length must be 20 and 0 in position 42.  
DECIMAL Definition specification. P in position 40 or blank in position 40 for a non-subfield, 0 through 30 in position 41,42.

OR

Defined as numeric on non-definition specification.

Maximum length of 16 (precision 30) and maximum scale of 30.
NUMERIC Definition specification. S in position 40 or blank in position 40 for a subfield, 0 through 30 in position 41,42. Maximum length of 30 (precision 30) and maximum scale of 30.
FLOAT (single precision) Definition specification. F in position 40, length must be 4.  
FLOAT (double precision) Definition specification. F in position 40, length must be 8.  
CHAR(n) Definition specification. A or blank in positions 40 and blanks in position 41,42.

OR

Input field defined without decimal places.

OR

Calculation result field defined without decimal places.

n can be from 1 to 32766.
CHAR(n) Data structure name with no subfields in the data structure. n can be from 1 to 32766.
VARCHAR(n) Definition specification. A or blank in position 40 and VARYING in positions 44-80. n can be from 1 to 32740.
CLOB Not supported Use SQLTYPE keyword to declare a CLOB.
GRAPHIC(n) Definition specification. G in position 40.

OR

Input field defined with G in position 36.

n can be 1 to 16383.
VARGRAPHIC(n) Definition specification. G in position 40 and VARYING in positions 44-80. n can be from 1 to 16370.
DBCLOB Not supported Use SQLTYPE keyword to declare a DBCLOB.
BINARY Not supported Use SQLTYPE keyword to declare a BINARY.
VARBINARY Not supported Use SQLTYPE keyword to declare a VARBINARY.
BLOB Not supported Use SQLTYPE keyword to declare a BLOB.
DATE A character field

OR

Definition specification with a D in position 40.

OR

Input field defined with D in position 36.

If the format is *USA, *JIS, *EUR, or *ISO, the length must be at least 10. If the format is *YMD, *DMY, or *MDY, the length must be at least 8. If the format is *JUL, the length must be at least 6.
TIME A character field

OR

Definition specification with a T in position 40.

OR

Input field defined with T in position 36.

Length must be at least 6; to include seconds, length must be at least 8.
TIMESTAMP A character field

OR

Definition specification with a Z in position 40.

OR

Input field defined with Z in position 36.

Length must be at least 19; to include microseconds, length must be at least 26. If length is less than 26, truncation occurs on the microsecond part.
DATALINK Not supported  
ROWID Not supported Use SQLTYPE keyword to declare a ROWID.