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

This example shows how to select records with a specific date value using the Open Query File (OPNQRYF) command.

Assume that you want to process all records in which the Date field in the record is the same as the current date. Also assume that the Date field is in the same format as the system date. In a control language (CL) program, you can specify:
DCL        VAR(&CURDAT);  TYPE(*CHAR)  LEN(6)
RTVSYSVAL  SYSVAL(QDATE) RTNVAR(&CURDAT);
OVRDBF     FILE(FILEA) SHARE(*YES)
OPNQRYF    FILE(FILEA) QRYSLT('"' *CAT &CURDAT *CAT '" *EQ DATE')
CALL       PGM(PGMB)
CLOF       OPNID(FILEA)
DLTOVR     FILE(FILEA)

A CL variable is assigned with a leading ampersand (&) and is not enclosed in single quotation marks. The whole expression is enclosed in single quotation marks. The CAT operators and CL variable are enclosed in both single quotation marks and quotation marks.

It is important to know whether the data in the database is defined as character, date, time, timestamp, or numeric. In the preceding example, the Date field is assumed to be character.

If the DATE field is defined as date data type, the preceding example can be specified as:
OVRDBF FILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) QRYSLT('%CURDATE *EQ DATE')
CALL PGM(PGMB)
CLOF OPENID(FILEA)
DLTOVR FILE(FILEA)
Note: The date field does not have to have the same format as the system date.
You can also specify the example as:
DCL VAR(&CVTDAT); TYPE(*CHAR) LEN(6)
DCL VAR(&CURDAT); TYPE(*CHAR) LEN(8)
RTVSYSVAL SYSVAL(QDATE) RTNVAR(&CVTDAT);
CVTDAT DATE(&CVTDAT); TOVAR(&CURDAT); TOSEP(/)
OVRDBF FILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA)
       QRYSLT('"' *CAT &CURDAT *CAT '" *EQ DATE')
CALL PGM(PGMB)
CLOF OPNID (FILEA)
DLTOVR FILE(FILEA)
This is where DATE has a date data type in FILEA, the job default date format is MMDDYY, and the job default date separator is the slash (/).
Note: For any character representation of a date in one of the following formats, MMDDYY, DDMMYY, YYMMDD, or Julian, the job default date format and separator must be the same to be recognized.
If, instead, you were using a constant, the QRYSLT would be specified as follows:
QRYSLT('"12/31/87" *EQ DATE')
The job default date format must be MMDDYY and the job default separator must be the slash (/).
If a numeric field exists in the database and you want to compare it to a variable, only a character variable can be used. For example, to select all records where a packed Date field is greater than a variable, you must ensure that the variable is in character form. Normally, this means that before the OPNQRYF command, you use the Change Variable (CHGVAR) command to change the variable from a decimal field to a character field. The CHGVAR command would be specified as follows:
CHGVAR VAR(&CHARVAR);  VALUE('123188')
The QRYSLT parameter would be specified as follows (see the difference from the preceding examples):
QRYSLT(&CHARVAR  *CAT ' *GT DATE')
If, instead, you were using a constant, the QRYSLT statement would be specified as follows:
QRYSLT('123187 *GT DATE')