Y2K date solutions seem as varied as the vendors who offer them. One overlooked yet critical piece of logic in Y2K conversion is the Open Query File (OPNQRYF) command. Here are a few OPNQRYF techniques for those of you who have chosen windowing as your Y2K solution.
Many shops have chosen windowing as their solution to the Y2K issue. Consequently, one problem AS/400 sites inevitably encounter is how to use windowing with Open Query File (OPNQRYF). It is rare that an AS/400 shop does not have at least a few of these CL-based front-ends to assist in report processing, and rewriting OPNQRYF commands can escalate costs at a time when your completion deadline is nonnegotiable.
Typically, you use the OPNQRYF command to select a set of records based on certain criteria and place them in the proper sequence. Usually, a high-level language (HLL) program is called to process the data OPNQRYF selects. Common use of the 6-byte date field (YYMMDD) leads to obvious sorting errors after this data crosses the boundary between 1999 and 2000.
The two most common methods of addressing the Y2K problem are database conversion and windowing. Database conversion entails using the date data type, converting 6-byte fields (YYMMDD) to 8 bytes (YYYYMMDD), or some other accepted format. The object-to-database conversion alters the way dates are stored so dates sort chronologically.
In contrast, windowing leaves dates in a two-digit year format, assuming the dates are within a 100-year window (1950-2049, for example). In some cases, however, database conversion entails far more code changes than does windowing. For that reason, many shops have chosen windowing based on time considerations. This article discusses only the windowing conversion of OPNQRYF.
Original Query
To illustrate this technique, assume you have a file of inventory transactions keyed in by item number, as shown in Figure 1. (For this example, ITEMFILE in Figure 2 contains the data.) Figure 3 shows how OPNQRYF selects the data in the file by CHANGEDATE. This code is not ready for 2000.
OPNQRYF selects all records greater than the value passed in through parameter &TSTDATE. You call the ITEMQRYR program to produce a basic report of the records that OPNQRYF filters. Figure 4 shows the results for the original query if &TSTDATE has the value 901210. Note that the dates are printed in standard MMDDYY format.
The problem with OPNQRYF is that it doesnt select dates from the Year 2000 and beyond, but we know of several ways to fix that problem. Here are a couple.
Query Renovated Using %DATE
You can use the %DATE function to renovate an OPNQRYF command for Y2K. This function converts the date in the field CHANGEDATE. The reformatted CHANGEDATE is then compared with the CL variable &TSTDATE. This is illustrated in Figure 5.
The %DATE returns a date from a given value. Two criteria determine the format of the value: the system date separator and system date format. In the United States, the date separator character is typically the slash (/), and the usual date format is *MDY.
You need to make only two changes in the CL program to renovate it for Y2K. First, you change the format of the variable &TSTDATE and reformat the date to match the standards set by the system date separator and format. For example, &TSTDATE now reads 12/10/90 instead of 901210.
The second change is more complex. You need to create three mapped fields to produce the date field Y2KDATE (Y2KDATE is used in the greater than comparison instead of CHANGEDATE):
1. Y2KDATE1. The %DIGITS function converts packed field CHANGEDATE to a character type.
2. Y2KDATE2. Reformat Y2KDATE1 into MM/DD/YY format through substringing and concatenation. Note that this format must use the values of the system date separator and format (/ and *MDY in our example).
3. Y2KDATE. Create the final map field by using the %DATE function on Y2KDATE2. Use of the %DATE function forces a date to be evaluated using the range 1940-2039. Because the value of Y2KDATE occurs in the date window 1940-2039, any date in the proper format compared with it is also evaluated in the window.
The renovated query now selects any records greater than 12/10/1990, based on a 1940-2039 window (see Figure 6). However, a drawback to this approach is the current limit to using 1940-2039 as your date window. Many shops may use a different window or may want more precise control of their windows range.
Query Renovated Using a Mathematical Calculation
This version of Y2K renovation takes a mathematical approach. You use a simple formula to produce a century field with a value of 19 or 20. This field is then concatenated onto a date YYMMDD to produce a date field YYYYMMDD. The formula is shown below: Take the date 980101, for example (January 1, 1998). The decade of the year is the first digit in a two-digit year. For the year 98, the decade is 9. The pivot decade is the first digit in the two-digit window-beginning year, divided by 10. For a window of 1950-2049, the pivot decade is 0.5.
Therefore, to calculate the century field for January 1, 1998, the system calculates as follows:
1. Century = ((199 - 9) * .1 ) + .5
2. Century = 19.5
3. Century truncated = 19
4. Century appended to sample date = 19980101
Century = ((199 - Decade of Year) * .1) + Pivot Decade
As before, two changes must be applied to renovate the CL. (The revised code is shown in Figure 7.) The first is to alter the &TSTDATE field to the YYYYMMDD format. The second is more extensive.
Use MAP fields to produce a YYYYMMDD-formatted version of CHANGEDATE:
1. Y2KDATE1. The %DIGITS function converts CHANGEDATE to character
2. Y2KDECADE. The decade of the date is extracted.
3. Y2KCENTUR1. The century creation formula is applied. Because a pivot decade of 0.5 has been chosen, the date is forced into a 1950-2049 window.
4. Y2KCENTUR2. The result from Y2KCENTUR1 is truncated to two digits.
5. Y2KCENTURY. Y2KCENTUR2 is converted to a character field.
6. Y2KDATE. Y2KCENTURY and Y2KDATE1 are concatenated to produce a date format of YYYYMMDD.
The renovated query now selects any records greater than 12/10/1990 based on a 1950-2049 window (see Figure 8).
Processing Invalid Dates
A disadvantage of these two approaches is that they do not correctly process invalid date values, such as all 0s and all 9s. The %DATE function cannot process invalid date values whatsoever, and the mathematical approach allows invalid values but does not sequence them properly. Typically, a date of all 0s should appear at the beginning of reports and a date of all 9s, at the end.
However, if youre willing to introduce a little messy code into the MAPFLD parameter, you can account for these special dates.
The revised query in Figure 9 sorts the records by the new MAPFLD SORTFIELD but can easily be changed to do the previous record selection (the CL driver has been slightly modified):
1. Create a physical file WORKFILE with the same fields as ITEMFILE but add one more field, SORTFIELD, defined as 7 alphanumeric. WORKFILE does not contain data and does not have to contain a member.
2. Change the HLL program ITMQRYR to read from WORKFILE rather than from ITEMFILE. The program does not read WORKFILE; it only appears to do so.
3. Change the OPNQRYF to sort on SORTFIELD rather than on the date field.
4. In the MAPFLD parameter, define SORTFIELD as a modified version of CHANGDATE. SORTFIELD comprises other mapped fields:
SORT1. CHANGEDATE is divided by 999,999, and the result is truncated to a length of one digit. This division produces a 0 for all values except the date 99/99/99, which produces a 1.
SORTW. CHANGEDATE is divided by itself or one, whichever is greater, and the result is truncated to a length of one digit. This division produces a 1 for all date values except the date 00/00/00, which produces a 0.
SORT2. Adding 600,000 to CHANGEDATE and keeping the remainder after dividing by 1,000,000 results in a windowed date. Multiplying by SORTW causes dates of all 0s to remain 0 and nonzero dates to keep their windowed values.
Figure 10 shows the output for this OPNQRYF. For this example, two records using 00/00/00 and 99/99/99 have been added. The column entitled Unprinted Value of SORTFIELD illustrates the value of SORTFIELD at completion of the query.
Be Creative!
You can use different approaches to prepare OPNQRYF for Y2K. Windowing techniques can lead to a maintenance nightmare, but if time is running out, you can use these tips to maintain business functionality. Some of these methods may prove most practical to address the programs and processes your business relies upon, and you can
type.
find more techniques in the book Y2K Survival Kit for AS/400 Programmers published by Midrange Computing.
Whatever you do, dont be afraid to try unfamiliar techniques. Creativity may be the most important tool of all for riding out the Y2K storm.
A
A R ITEMREC
A ITEMNUM 10
A CHANGEDATE 6P 0
A ITEMTYPE 10
A ITEMDESC 40
A K ITEMNUM
Figure 1: Physical file ITEMFILE is typical of files used with OPNQRYF.
ITEMNUM CHANGEDATE ITEMTYPE ITEMDESC
9999 991231 BOX CARDBOARD 9999 000101 BOX CARDBOARD 9999 900101 BOX CARDBOARD 9999 490101 BOX CARDBOARD 9999 500101 BOX CARDBOARD 9999 891225 BOX CARDBOARD 9999 690101 BOX CARDBOARD 9999 110228 BOX CARDBOARD 9999 990315 BOX CARDBOARD 9999 050615 BOX CARDBOARD 9999 100917 BOX CARDBOARD 9999 220503 BOX CARDBOARD 9999 300930 BOX CARDBOARD
Figure 2: ITEMFILE contains the following data.
PGM PARM(&TSTDATE)
DCL VAR(&TSTDATE) TYPE(*CHAR) LEN(6)
OVRDBF FILE(ITEMFILE) TOFILE(*LIBL/ITEMFILE) +
SHARE(*YES)
OPNQRYF FILE((ITEMFILE)) + QRYSLT('CHANGEDATE *GT ' *CAT &TSTDATE)
CALL PGM(ITEMQRYR)
CLOF OPNID(ITEMFILE)
DLTOVR FILE(ITEMFILE)
ENDPGM
Figure 3: This OPNQRYF routine is not ready for 2000.
ITEMNUM CHANGEDATE ITEMTYPE ITEMDESC
9999 12/31/99 BOX CARDBOARD 9999 03/15/99 BOX CARDBOARD
Figure 4: OPNQRYF incorrectly selects 20
th-century dates only.
PGM DCL VAR(&TSTDATE) TYPE(*CHAR) LEN(8) VALUE('12/10/90')
OVRDBF FILE(ITEMFILE) TOFILE(*LIBL/ITEMFILE) +
SHARE(*YES)
OPNQRYF FILE((ITEMFILE)) +
QRYSLT('Y2KDATE *GT "' *CAT &TSTDATE *CAT '"') +
MAPFLD((Y2KDATE1 '%DIGITS(CHANGEDATE)' *CHAR 6) +
(Y2KDATE2 '%SST(Y2KDATE1 3 2) *CAT "/" *CAT +
%SST(Y2KDATE1 5 2) *CAT "/" *CAT +
%SST(Y2KDATE1 1 2)' *CHAR 8) +
(Y2KDATE '%DATE(Y2KDATE2)'))
CALL PGM(ITEMQRYR)
CLOF OPNID(ITEMFILE)
ENDPGM
Figure 5: This query has been renovated using the %DATE function.
ITEMNUM CHANGEDATE ITEMTYPE ITEMDESC
9999 12/31/99 BOX CARDBOARD 9999 01/01/00 BOX CARDBOARD 9999 02/28/11 BOX CARDBOARD 9999 03/15/99 BOX CARDBOARD 9999 06/15/05 BOX CARDBOARD 9999 09/17/10 BOX CARDBOARD 9999 05/03/22 BOX CARDBOARD 9999 09/30/30 BOX CARDBOARD
Figure 6: Using the %DATE function makes OPNQRYF use the 1940-2039 window.
PGM DCL VAR(&TSTDATE) TYPE(*CHAR) LEN(8) VALUE('19901210')
OVRDBF FILE(ITEMFILE) TOFILE(*LIBL/ITEMFILE) +
SHARE(*YES)
OPNQRYF FILE((ITEMFILE)) +
QRYSLT('Y2KDATE *GT "' *CAT &TSTDATE *CAT '"') +
MAPFLD((Y2KDATE1 '%DIGITS(CHANGEDATE)' *CHAR 6) +
(Y2KDECADE '%SST(Y2KDATE1 1 1)' *ZONED 1) +
(Y2KCENTUR1 '(((199 - Y2KDECADE) * .1) + .5)' +
*ZONED 3 1) +
(Y2KCENTUR2 Y2KCENTUR1 *ZONED 2) +
(Y2KCENTURY Y2KCENTUR2 *CHAR 2) +
(Y2KDATE 'Y2KCENTURY *CAT Y2KDATE1'))
CALL PGM(ITEMQRYR)
CLOF OPNID(ITEMFILE)
ENDPGM
Figure 7: A mathematical calculation renovates this query.
ITEMNUM CHANGEDATE ITEMTYPE ITEMDESC
9999 12/31/99 BOX CARDBOARD 9999 01/01/00 BOX CARDBOARD 9999 01/01/49 BOX CARDBOARD 9999 02/28/11 BOX CARDBOARD 9999 03/15/99 BOX CARDBOARD 9999 06/15/05 BOX CARDBOARD 9999 09/17/10 BOX CARDBOARD 9999 05/03/22 BOX CARDBOARD 9999 09/30/30 BOX CARDBOARD
Figure 8: Using this math formula, OPNQRYF uses the 1950-2049 window.
PGM OPNQRYF FILE((ITEMFILE)) FORMAT(ITEMFILTEM) +
KEYFLD((SORTFIELD)) +
MAPFLD((SORT1 'CHANGEDATE / 999999' *DEC 1 0) +
(SORTW 'CHANGEDATE / %MAX(CHANGEDATE 1)' *DEC 1 0) +
(SORT2 'SORTW * ((CHANGEDATE + 600000) // 1000000)' +
*DEC 6 0) +
(SORTFIELD '%DIGITS(SORT1) *CAT %DIGITS(SORT2)'))
OVRDBF FILE(WORKFILE) TOFILE(ITEMFILE) SHARE(*YES)
CALL PGM(ITEMQRYR)
CLOF OPNID(ITEMFILE)
ENDPGM
Figure 9: A mathematical calculation using MAP fields renovates this query.
ITEMNUM CHANGEDATE Unprinted ITEMTYPE ITEMDESC
Value of SORTFIELD 9999 00/00/00 0000000 BOX CARDBOARD 9999 01/01/49 0090101 BOX CARDBOARD 9999 01/01/50 0100101 BOX CARDBOARD 9999 01/01/69 0290101 BOX CARDBOARD 9999 12/25/89 0491225 BOX CARDBOARD 9999 01/01/90 0500101 BOX CARDBOARD 9999 03/15/99 0590315 BOX CARDBOARD 9999 12/31/99 0591231 BOX CARDBOARD 9999 01/01/00 0600101 BOX CARDBOARD 9999 06/15/05 0650615 BOX CARDBOARD 9999 09/17/10 0700917 BOX CARDBOARD 9999 02/28/11 0710228 BOX CARDBOARD 9999 05/03/22 0820503 BOX CARDBOARD 9999 09/30/30 0900930 BOX CARDBOARD 9999 99/99/99 1599999 BOX CARDBOARD
LATEST COMMENTS
MC Press Online