Tips and Techniques: Displaying Dates in Excel

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

What? Isn't this an RPG column? It sure is. But copying data from the iSeries to CSV format for reading in MS Excel is extremely widespread throughout the iSeries community.

So let's start out with some basics. First, the most often used method of copying DB2/400 data to CSV format is the OS/400 CPYTOIMPF command. This command--although slow, narrow in scope, and not very versatile--does convert data into CSV format so that it can be opened in MS Excel.

The reason CPYTOIMPF is limited is that it outputs fixed-length field values (it does not trim off trailing blanks), it does not provide a method to format numeric or date output (however, it does support converting dates to one consistent format), and it does not have an option to generate a row of column headings based on the field names or the DDS COLHDG keyword.

And perhaps the worst thing of all about CPYTOIMPF is that as the number of records in the file that are to be converted grows, performance of the command seems to fall off a cliff. Certainly, more records means more time to convert, but the curious design of CPYTOIMPF obviously wasn't meant for a large-volume conversion.

The bottom line: Use CPYTOIMPF for quick copies to CSV format, when you just need the raw data without column headings and totals and when you don't mind trailing blanks in the output fields.

Once you have your data in CSV format, you can edit it so that it displays better in MS Excel. For example, adding a header row, perhaps reformatting a date column, and of course adding column totals are often necessary. In addition, avoiding zero suppression is always fun.

In MS Excel, functions can be used to format individual cells, and adding column headings is only a matter of inserting an empty row and then typing in the column headings.

Two key areas have been problems for iSeries data moved to CSV format and opened in MS Excel: date values and numeric values.

Date values in Excel are somewhat odd anyway, but there is a function that allows you to embed a date value in a cell and keep it in date format. Normally, when CPYTOIMPF copies a date value, it converts that value to text in *ISO format (yyyy-mm-dd) and encloses it in quotes.

This isn't bad, and Excel displays the date value correctly. But that's not really what you want because you're not telling Excel that the field is a date data type. Fortunately, Excel has a DATE function that can be used to format a set of data so that it is not only displayed as a date, but interpreted as a date by Excel. This allows data sets to be sorted correctly from within Excel.

To use the DATE function, you need to write the date value to the CSV file as follows, where yyyy is the two- or four-digit year, mm is the two-digit month, and dd is the two-digit day of the month::

=DATE(yyyy,mm,dd)

Yes, adding MS Excel functions into a CSV works fine. Excel parses functions it detects in CSV files as if they had come from an actual Excel file. So if a CSV file contained this data...

02345,"XYZ Company","2005-07-31"

...it could be changed to this...

02345,"XYZ Company",=DATE(2005,07,31)

...and the date would now be identified as a date.

The next thing we want to do is correct the problem with the number 02345. In Excel, this numeric value is normally displayed as 2345. But what if we don't want to suppress leading or trailing zeroes in the value? Enter the "quote" function, which looks like this:

=("02345")

Whatever value is enclosed in the quote function is displayed. So 034.50, which would normally be displayed as 34.5, is displayed as 034.50.

There are several other options for converting DB2 database files to Excel besides CPYTOIMPF. In fact, the POI interface (class library) for Java is pretty robust and allows you write directly to an Excel file. I don't particularly like POI on the iSeries simply because (a) it requires Java and consequently requires that I load the JVM, and (b) it's overkill for most of the conversion I've ever come across.

In addition to POI, there are several third-party and free solutions out there. Some perform well; some do not. Most of the non-free solutions work effectively, but many of the "free" solutions work only in very limited scope (although the authors believe they work in all situations, I have yet to find one that works on my test files).

Of course, CPYTOCSV (Copy to CSV File) in the RPG xTools is available free, but requires the RPG xTools runtime.

Of the chargeable third-party solutions, some allow you to output column headings, and CPYTOCSV also allows you to convert dates to the =DATE function, convert numbers to the =("...") function, and insert column totals (=SUM function).

So fear not! Dates can be converted to Excel properly.

Bob Cozzi is a programmer/consultant, writer/author, and software developer of the RPG xTools, a popular add-on subprocedure library for RPG IV. His book The Modern RPG Language has been the most widely used RPG programming book for nearly two decades. He, along with others, speaks at and runs the highly-popular RPG World conference for RPG programmers.

BOB COZZI

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.


MC Press books written by Robert Cozzi available now on the MC Press Bookstore.

RPG TnT RPG TnT
Get this jam-packed resource of quick, easy-to-implement RPG tips!
List Price $65.00

Now On Sale

The Modern RPG IV Language The Modern RPG IV Language
Cozzi on everything RPG! What more could you want?
List Price $99.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • 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.

  • 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

  • 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: