Example 7: Select records using the Open Query File (OPNQRYF) command

This example shows how to map fields for packed numeric data fields using the Open Query File (OPNQRYF) command.

Assume that you have a packed decimal Date field in the format MMDDYY and you want to select all the records for the year 1988. You cannot select records directly from a portion of a packed decimal field, but you can use the MAPFLD parameter on the OPNQRYF command to create a new field that you can then use for selecting part of the field.

The format of each mapped field definition is:

(result field 'expression' attributes)

where:
result field
The name of the result field.
expression
How the result field should be derived. The expression can include substring, other built-in functions, or mathematical statements.
attributes
The optional attributes of the result field. If no attributes are given (or the field is not defined in a file), the OPNQRYF command calculates a field attribute determined by the fields in the expression.
OVRDBF     FILE(FILEA) SHARE(*YES)
OPNQRYF    FILE(FILEA) QRYSLT('YEAR *EQ "88" ') +
             MAPFLD((CHAR6 '%DIGITS(DATE)')  +
               (YEAR '%SST(CHAR6 5 2)' *CHAR 2))
CALL       PGM(PGMC)
CLOF       OPNID(FILEA)
DLTOVR     FILE(FILEA)
In this example, if DATE was a date data type, it can be specified as follows:
OPNQRYF FILE(FILEA) +
QRYSLT ('YEAR *EQ 88') +
MAPFLD((YEAR '%YEAR(DATE)'))

The first mapped field definition specifies that the Char6 field be created from the packed decimal Date field. The %DIGITS function converts from packed decimal to character and ignores any decimal definitions (that is, 1234.56 is converted to '123456'). Because no definition of the Char6 field is specified, the system assigns a length of 6. The second mapped field defines the Year field as type *CHAR (character) and length 2. The expression uses the substring function to map the last 2 characters of the Char6 field into the Year field.

Note that the mapped field definitions are processed in the order in which they are specified. In this example, the Date field was converted to character and assigned to the Char6 field. Then, the last two digits of the Char6 field (the year) were assigned to the Year field. Any changes to this order would have produced an incorrect result.
Note: Mapped field definitions are always processed before the QRYSLT parameter is evaluated.
You can accomplish the same result by specifying the substring on the QRYSLT parameter and dropping one of the mapped field definitions as follows:
OPNQRYF    FILE(FILEA) +
             QRYSLT('%SST(CHAR6 5 2) *EQ "88" ') +
             MAPFLD((CHAR6 '%DIGITS(DATE)'))