13
Sat, Apr
5 New Articles

Query/400 Date Manipulation

General
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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) ____________________ __ __ _________________________________ ____________________ _________________________________ ____________________ 
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: