OPNQRYF does not have a function that returns the day of week, but you can create your own. OPNQRYF has a %DAYS function, which returns the ordinal value of a date (number of consecutive days) since January 1 of the year A.D. 1. If you subtract the ordinal values of two dates, you find the number of days between the two dates. Dividing by seven tells how many weeks there are between the two dates.
The really interesting thing, however, is that the remainder of the division is the number of weekdays the first date is offset from the second. If the remainder is 0, the two dates are on the same day of the week. If the remainder is 1, the first date is on the weekday following the second. If the remainder is 6, the first date is on the weekday preceding the second. If the remainder is negative, the first date is before the second, and you must add 7 to it to determine the day of the week.
The second date, then, must be a date that falls on whichever day you want to be the standard. If you use January 7, 1940, as the second date, the standard will be Sunday, because January 7, 1940, fell on a Sunday.
6 illustrates how to determine the day of week with OPNQRYF. One of the fields in file MYDATA is JDATE, a six-digit packed decimal field that stores a date in YYMMDD format. OPNQRYF formats the data according to the record layout of file DAYSPF, which contains the fields in MYDATA and a one-digit packed decimal field called DAY.
Figure 6 illustrates how to determine the day of week with OPNQRYF. One of the fields in file MYDATA is JDATE, a six-digit packed decimal field that stores a date in YYMMDD format. OPNQRYF formats the data according to the record layout of file DAYSPF, which contains the fields in MYDATA and a one-digit packed decimal field called DAY.
OPNQRYF converts JDATE to the character field CDATE and adds editing slashes to put the date into MM/DD/YY format in field EDATE. Then it determines the number of days between EDATE and January 7, 1940, divides by seven, and stores the remainder in field DAY. DAY has a value from 0 (Sunday) to 6 (Saturday).
You may prefer to have OPNQRYF return a value from 1 to 7 instead. If so, change the calculation of DAY so it adds 1 to the remainder. You may also prefer to use a day other than Sunday as the base day. If so, change the second date in the calculations for DAY to some date that falls on the desired base day.
- Ted Holt
TechTalk: Determining the day of the week with OPNQRYF.
Figure 6: Determining the Day of Week with OPNQRYF
/* Field JDATE in DAYSPF is a 6-digit packed + decimal field in YYMMDD format */ OVRDBF FILE(DAYSPF) TOFILE(MYDATA) SHARE(*YES) OPNQRYF FILE((MYDATA)) + FORMAT(DAYSPF) + MAPFLD((CDATE '%DIGITS(JDATE)' *CHAR 6) + (EDATE '%SST(CDATE 3 2) *CAT "/" *CAT + %SST(CDATE 5 2) *CAT "/" *CAT + %SST(CDATE 1 2)') + (DAY '(%DAYS(EDATE) - %DAYS("01/07/40")) // 7')) CALL DAYSR /* written as if it will read DAYSPF */ CLOF OPNID(MYDATA) DLTOVR FILE(DAYSPF)
LATEST COMMENTS
MC Press Online