When IBM first announced the new date data types, I looked to them with great anticipation. Finally, AS/400 programmers would have the ability to use intrinsic date manipulation commands on their databases.
Unfortunately, IBM's implementation did not include RPG programming support. I felt let down, as if a powerful programming capability had just been placed totally out of my reach. Or so I thought. I have since discovered a way to convert numeric date fields into date data types and manipulate them with Query/400 for reporting purposes. The same technique can be used in other query utilities including SQL/400, SQL/400 Query Manager, Query Management/400 and the Open Query File (OPNQRYF) command. (The syntax rules will vary depending on which tool you use.)
Date Data Types
First, let's review what date data fields can look like. 1 shows the format of the various date data type fields. Query/400 can use any of the SAA date formats listed in the table or the AS/400 date format that matches your job's date format.
First, let's review what date data fields can look like. Figure 1 shows the format of the various date data type fields. Query/400 can use any of the SAA date formats listed in the table or the AS/400 date format that matches your job's date format.
For the purposes of this article, I've adopted the MDY format. If you choose one of the SAA date formats listed at the top of 1, Query/400 allows you to deal with four-digit years. This is a good way of starting to address the problems created by the year 2000.
For the purposes of this article, I've adopted the MDY format. If you choose one of the SAA date formats listed at the top of Figure 1, Query/400 allows you to deal with four-digit years. This is a good way of starting to address the problems created by the year 2000.
How to Use Them
Let's start with an easy example. Suppose your company's inventory production manager comes to you and requests a report of past-due customer orders. The logic is quite simple; all you need to do is to select those orders with scheduled ship dates prior to today's date.
Since this report would be run daily, it would be rather inconvenient to have to change the query definition every day to specify the new past due comparison date. Fortunately, Query/400 provides the ability to create a date field that contains the current system date. You can then use this field as the value to compare against.
2 provides an example of how to do this using the Define Result Fields display. Since you are creating an actual date type field, it is not necessary to define the length of the field you created.
Figure 2 provides an example of how to do this using the Define Result Fields display. Since you are creating an actual date type field, it is not necessary to define the length of the field you created.
But how do you convert the value in the Order Due Date field so it can be compared to TODAY? First, let's make an assumption; the due date (DUEDT) is a six-byte, numeric field with a YMD format. (Whatever the format your data field is in, you can still use the basic principles presented in this article.) To create a date type field from DUEDT, you must first turn it into a character field, segment it and insert separators.
To convert a numeric field to a character type field, use the digits function as shown in 3.
To convert a numeric field to a character type field, use the digits function as shown in Figure 3.
Once the date is in character format, you'll insert the date separator characters and convert the date to MDY format using the substring function as shown at the bottom of 3.
Once the date is in character format, you'll insert the date separator characters and convert the date to MDY format using the substring function as shown at the bottom of Figure 3.
The last step is to convert the character field containing the separator characters (DUEDTS in our example) to a date data type. To do this, use the date function as shown in 4. With these statements, you have converted a numeric field into a date data type field in the MM/DD/YY format.
The last step is to convert the character field containing the separator characters (DUEDTS in our example) to a date data type. To do this, use the date function as shown in Figure 4. With these statements, you have converted a numeric field into a date data type field in the MM/DD/YY format.
You could also convert the DUEDT field using just one statement, bypassing the creation of the DUEDTC and DUEDTS fields as shown in 5. I don't recommend this practice, though. You end up calling the digits function three times just to save the effort of creating two fields.
You could also convert the DUEDT field using just one statement, bypassing the creation of the DUEDTC and DUEDTS fields as shown in Figure 5. I don't recommend this practice, though. You end up calling the digits function three times just to save the effort of creating two fields.
To report on all orders that are past due, simply code your Select Records display as shown in 6.
To report on all orders that are past due, simply code your Select Records display as shown in Figure 6.
The production manager really likes the report you've just created. Since your company has a seven-day lead time to fulfill its orders, he now wants your report to show orders due to be shipped in the next seven days as well as those that are past due. 7 shows how to calculate the comparison date.
The production manager really likes the report you've just created. Since your company has a seven-day lead time to fulfill its orders, he now wants your report to show orders due to be shipped in the next seven days as well as those that are past due. Figure 7 shows how to calculate the comparison date.
Now all you do is substitute the PLUS7 field for the TODAY field in the Select Records display and your report is done.
Date Field Operations
You now know that Query/400 allows a date data type field to be compared to any other date field, no matter how you've defined it. Query/400 also enables you to perform date calculations on this type of field, as you will see next. 8 shows the available date field operations.
You now know that Query/400 allows a date data type field to be compared to any other date field, no matter how you've defined it. Query/400 also enables you to perform date calculations on this type of field, as you will see next. Figure 8 shows the available date field operations.
Suppose your Human Resources manager drops by your office the next day and asks you to produce another report. She wants this report to list those employees who have been with the company for an exact multiple of five years. She intends to use this report for employee awards.
9 shows how to accomplish this. Purely for performance reasons, I suggest you multiply by .2 instead of dividing by 5, as the figure illustrates.
Figure 9 shows how to accomplish this. Purely for performance reasons, I suggest you multiply by .2 instead of dividing by 5, as the figure illustrates.
Advanced Uses
Later the same day, the Accounts Receivable manager comes to you and asks if you can modify the Aging report to allow selection criteria. If your manager doesn't mind a Query-formatted report, 10 shows how you can do it.
Later the same day, the Accounts Receivable manager comes to you and asks if you can modify the Aging report to allow selection criteria. If your manager doesn't mind a Query-formatted report, Figure 10 shows how you can do it.
The DAYS function is used to return a numeric representation of the two dates (TODAY and DUEDTD) so the difference between these dates can be calculated and placed into field DIFFDAYS. The trick here is the creation of the field THIRTY. Since it is a zero-decimal field, it changes value only every 30 days, which is exactly what you want. To get the report to break and summarize correctly, you must select the field THIRTY-but, in the Field Specifications area, specify a length of zero bytes. This tells Query/400 not to print this field.
The inventory production manager just left your office complaining that he is now required to provide the on-time shipping performance numbers. To fulfill the request, all you need to do is calculate the difference (in number of days) between the scheduled ship date and the actual ship date, as shown in 11.
The inventory production manager just left your office complaining that he is now required to provide the on-time shipping performance numbers. To fulfill the request, all you need to do is calculate the difference (in number of days) between the scheduled ship date and the actual ship date, as shown in Figure 11.
Unfortunately, this report will not be very meaningful if it is run against all of the orders for the month. It makes sense to break the report up into two parts. Part one lists those orders that shipped on time or early, and part two lists those orders that shipped late. If you include a record count in each report, you can then calculate the percentage of on-time shipments.
Now your Accounts Receivable manager is back in your office and would like to know if you could produce a report listing each customer's average number of days to pay. 12 illustrates this technique.
Now your Accounts Receivable manager is back in your office and would like to know if you could produce a report listing each customer's average number of days to pay. Figure 12 illustrates this technique.
All you need to do is use the summary function, avg, on DIFFDAYS and summarize by customer. Your report is done.
A Solution for the Interim
The RPG date support I expected when the date data types were first announced is actually coming in the form of ILE RPG/400. Look for "An Introduction to ILE RPG: Part 5" in next month's issue for more information on date field support in ILE RPG. But in case your shop does not immediately adopt ILE RPG as its operative language, you're not left without the capacity to use date data types.
In the meantime, you can use Query/400 or other query utilities to manipulate date fields for reports. As you've seen throughout the examples here, the key to using date fields with a query tool is to create a character field in the correct format. And, although not addressed here, time fields are manipulated in the same way; they just use a different separator. I believe you will find many more uses for these fields as you become comfortable with them.
Bill Robins has been working with midrange computers for 12 years. He can be reached through the MC-BBS.
Query/400 Date Manipulation
Figure 1 Valid Query/400 Date Formats
Name SAA Format Layout Length Example International Standards Y ISO yyyy-mm-dd 10 1993-12-31 Organization USA Standard Y USA mm/dd/yyyy 10 12/31/1993 European Standard Y EUR dd.mm.yyyy 10 12.31.1993 Japanese Industrial Y JIS yyyy-mm-dd 10 1993-12-31 Standard AS/400 Format N DMY dd/mm/yy 8 31/12/93 YMD yy/mm/dd 8 93/12/31 MDY mm/dd/yy 8 12/31/93 JUL yy/ddd 6 93/365
Query/400 Date Manipulation
Figure 2 Creating a Data Type
Define Result Fields Field Expression Column Heading Len Dec TODAY current(date) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________
Query/400 Date Manipulation
Figure 3 Using the Digits Function
Define Result Fields Field Expression Column Heading Len Dec DUEDTC digits(duedt) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________ DUEDTS substr(duedtc,3,2) |||||||| '/' |||||||| ___________________ substr(duedtc,5,2) |||||||| '/' |||||||| ________ substr(duedtc,1,2) ____________________
Query/400 Date Manipulation
Figure 4 Using the Date Function
Define Result Fields Field Expression Column Heading Len Dec DUEDTD date(duedts) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________
Query/400 Date Manipulation
Figure 5 Converting a Field in One Statement
Define Result Fields Field Expression Column Heading Len Dec DUEDTD date(digits(substr(duedt),3,2)||||||||' ____________________ __ /'||||||||(digits(substr(duedt),5,2)||||||||' ________ /'||||||||(digits(substr(duedt),1,2) ______________
Query/400 Date Manipulation
Figure 6 Selecting "Past Due" Records
Select Records AND/OR Field Test Value (Field, Number, 'Characters', or ...) DUEDTD LT TODAY
Query/400 Date Manipulation
Figure 7 Adding Seven Days to the Current Date
Define Result Fields Field Expression Column Heading Len Dec PLUS7 current(date) + 7 days ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________
Query/400 Date Manipulation
Figure 8 Date Field Operations
UNABLE TO REPRODUCE GRAPHICS
Query/400 Date Manipulation
Figure 9 Selecting Five-year Employees
Define Result Fields Field Expression Column Heading Len Dec HIREDTC digits(HIREDT) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________ HIREDTS substr(HIREDTC,3,2) |||||||| '/' |||||||| ___________________ substr(HIREDTC,5,2) |||||||| '/' |||||||| ________ substr(HIREDTC,1,2) ____________________ HIREDTD date(HIREDTS) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________ DIFFYEARS year(current(date)) - ____________________ __ __ year(HIREDTD) ____________________ _________________________________ ____________________ FIVEWHOLE DIFFYEARS * .2 ____________________ 4 0 _________________________________ ____________________ _________________________________ ____________________ FIVEEXACT DIFFYEARS * .2 ____________________ 5 1 _________________________________ ____________________ _________________________________ ____________________ DIFF FIVEWHOLE - FIVEEXACT ____________________ 2 1 _________________________________ ____________________ _________________________________ ____________________ Select Records AND/OR Field Test Value (Field, Number, 'Characters', or ...) DIFF EQ 0 ____ ______________ _____ ______________________________
Query/400 Date Manipulation
Figure 10 Enabling the Aging Report with Selection Criteria
Define Result Fields Field Expression Column Heading Len Dec TODAY current(date) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________ DUEDTC digits(duedt) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________ DUEDTS substr(duedtc,3,2) |||||||| '/' |||||||| ___________________ substr(duedtc,5,2) |||||||| '/' |||||||| ________ substr(duedtc,1,2) ____________________ DUEDTD date(duedts) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________ DIFFDAYS DAYS(TODAY) - DAYS(DUEDTD) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________ THIRTY DIFFDAYS / 30 ____________________ 4 0 _________________________________ ____________________ _________________________________ ____________________
Query/400 Date Manipulation
Figure 11 Calculating the Difference Between Two Dates
Define Result Fields Field Expression Column Heading Len Dec DUEDTC digits(duedt) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________ DUEDTS substr(duedtc,3,2) |||||||| '/' |||||||| ___________________ substr(duedtc,5,2) |||||||| '/' |||||||| ________ substr(duedtc,1,2) ____________________ DUEDTD date(duedts) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________ SHIPDTC digits(SHIPDT) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________ SHIPDTS substr(SHIPDTC,3,2) |||||||| '/' |||||||| ___________________ substr(SHIPDTC,5,2) |||||||| '/' |||||||| ________ substr(SHIPDTC,1,2) ____________________ SHIPDTD date(SHIPDTS) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________ DIFFDAYS DAYS(DUEDTD) - DAYS(SHIPDTD) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________
Query/400 Date Manipulation
Figure 12 Calculating Average Days to Pay
Define Result Fields Field Expression Column Heading Len Dec DUEDTC digits(duedt) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________ DUEDTS substr(duedtc,3,2) |||||||| '/' |||||||| ___________________ substr(duedtc,5,2) |||||||| '/' |||||||| ________ substr(duedtc,1,2) ____________________ DUEDTD date(duedts) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________ PAYDTC digits(PAYDT) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________ PAYDTS substr(PAYDTC,3,2) |||||||| '/' |||||||| ___________________ substr(PAYDTC,5,2) |||||||| '/' |||||||| ________ substr(PAYDTC,1,2) ____________________ PAYDTD date(PAYDTS) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________ DIFFDAYS DAYS(PAYDTD - DAYS(DUEDTD) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________
LATEST COMMENTS
MC Press Online