fhg
Volume 9, Number 26 -- August 19, 2009

Formatting Dates with SQL

Published: August 19, 2009

by Ted Holt


Note: The code accompanying this article is available for download here.


The famous ERP package with which I work at my day job stores dates in seven-digit packed decimal fields in CYYMMDD format. Reformatting these dates into something readable in an SQL query is challenging, to put it mildly. To complicate matters, our homegrown systems have dates in YYYYMMDD formats, and outfiles of CL commands have dates in MMDDYY format. I finally got tired of converting and substringing and concatenating, and decided to write a general SQL function to convert dates. I hastily threw it together, but it seems to be working fine. Feel free to suggest improvements.

Before we look at my function, let's look more closely at the problem. Suppose I want to reformat a CYYMMDD packed decimal field named CRDATE into MM/DD/YYYY format. How would I do that, using only the tools SQL provides me? I'd probably do something like this:

select whatever,
  substr( digits(dec(19000000+crdate,8,0)), 5, 2 ) || '/' ||
  substr( digits(dec(19000000+crdate,8,0)), 7, 2 ) || '/' ||
  substr( digits(dec(19000000+crdate,8,0)), 1, 4 )
 from mylib/myfile

Rotten, huh?

I have written about this problem before. My XDATE function is one solution. But I decided I wanted a more generic solution. I wanted a function that would convert a numeric date in any format to any other format. So I wrote the FMTDATE function.

FMTDATE requires three parameters: a numeric value that represents a date; the format in which the numeric date is stored; and the format into which the date is to be converted. The result of the function is a 10-character variable-length value.

The following date formats are supported:


MDY

DMY

YMD

JUL

LONGJUL

CMDY

CDMY

CYMD

ISO, JIS, YYMD

EUR, DMYY

USA, MDYY


The format ID's are not case sensitive.

Here's the previous example, using the FMTDATE function:

select whatever, fmtdate(CRDATE,'CYMD','MDYY/')
 from mylib/myfile

Here are a few thoughts to keep in mind when using FMTDATE.

  1. If a date is invalid, FMTDATE returns null.
  2. You may place a leading asterisk on a format parameter, as in RPG, if you like. That is to say, MDY and *MDY are equivalent.
  3. You may specify a separator character at the end of a format, but it will be ignored for the input date.
  4. The ampersand separator character represents a blank.
  5. Unlike RPG, no separator character is implied for ISO, EUR, JIS, and USA dates.
  6. Unlike RPG, you can use any character except for a letter as a separator character.

To create and use this function, follow these steps, replacing xxx with your library of choice. I suggest you place the service program and SQL function in a generally available library, such as QGPL.

1. Place the source code into the FMTDATE RPG source member. The source code is attached to this article. Use FTP or some other file transfer utility to load it into the source physical file member.

2. Compile the source member into a module.

CRTRPGMOD MODULE(xxx/FMTDATE)
          SRCFILE(xxx/QRPGLESRC)
          SRCMBR(FMTDATE)

3. Create a service program from the module.

CRTSRVPGM SRVPGM(xxx/FMTDATE)
          MODULE(xxx/FMTDATE)
          EXPORT(*ALL)

4. In SQL, create a function to run the service program.

create function xxx/fmtdate
(inDate dec(8,0), inFromFmt varchar(8), inToFmt varchar(8))
returns varchar(10)
language rpgle
parameter style general with nulls
deterministic
no sql
returns null on null input
no external action
not fenced
no final call
allow parallel
no scratchpad
external name 'XXX/FMTDATE(FMTDATE)'

Have fun. Let me know if you find any bugs or have suggestions for improvement.


Send your comments to Ted via the IT Jungle Contact page.


RELATED STORY

Omit Commas from Numeric Dates



                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
SYSTEM i DEVELOPER

Upgrade Your Skills at the Summit
- Quality training, excellent value -

Come to the RPG & DB2 Summit Oct 13-15 in Minneapolis for three full days of intensive education focused on RPG IV, SQL, DB2, PHP, V6R1, ILE, RSE/WDSC/RDi, the Web & more.

Learn the latest in practical, usable tips and techniques from top gurus Susan Gantner,
Skip Marchesani, Jon Paris, Paul Tuohy, Scott Klement
and others in a fun, highly interactive, invigorating environment.

Early Registration is just $995 thru Aug 31 and includes 7 meals; hotel is just $99 per night.

Check out the sessions & register today!


Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Erwin Earley, Brian Kelly, Michael Sansoterra
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

VAULT400:  White paper: National bike retailer "rolls" with Vault400
Raz-Lee Security:  iSecurity Compliance Evaluator: Instant network-wide compliance checks
COMMON:  Celebrate our 50th anniversary at annual conference, May 2 - 6, 2010, in Orlando


 

IT Jungle Store Top Book Picks

Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
The iSeries Express Web Implementer's Guide: List Price, $49.95
The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
Can the AS/400 Survive IBM?: List Price, $49.00
Chip Wars: List Price, $29.95


 
The Four Hundred
RPG: A Great Language with a Greater History

IBM Tweaks Power 570, 595 Deals Yet Again

Reconsidering SAN in Wake of SCSI Disk's End

As I See It: Daniel, Part Two

A Quick Analysis of Business Intelligence Planning

Four Hundred Stuff
ASTI Sees Promise in Plasmon's UDO Technology

Stonebranch Bolsters i OS Support in Workload Automation Tools

Data Control Issues Bring vLegaci QuickerApps to Market

Oracle Gives JDE More Supply Chain Planning Brains

Infor Snaps Up SoftBrands, Gets i OS-Based Hotel Suite

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

System i PTF Guide
August 15, 2009: Volume 11, Number 33

August 8, 2009: Volume 11, Number 32

August 1, 2009: Volume 11, Number 31

July 25, 2009: Volume 11, Number 30

July 18, 2009: Volume 11, Number 29

July 11, 2009: Volume 11, Number 28

July 4, 2009: Volume 11, Number 27

TPM at The Register
Big Blue bundles pound down mainframe prices

Uncle Sam shells out $62m for 100GbE

Novell lands full-time staff on openSUSE

Parallels juggles servers for John Q. ISP

Citrix touts XenServer hypervisor ramp

openSUSE will sacrifice 10.3 release on Halloween

Oracle spells out VM tools plans

Sun sets on Solaris Express Community Edition

NEC punts 0% financing, deferred payments

System sales might have hit bottom

HP sued by own sales reps

BMC snaps up message queue maven

IT shops struggle to control personnel costs

Sun's Rock is barefoot on Abbey Road

THIS ISSUE SPONSORED BY:

Help/Systems
System i Developer
Twin Data


Printer Friendly Version


TABLE OF CONTENTS
Validate DBCS-Open Data

Formatting Dates with SQL

Admin Alert: Correcting and Expanding the Program to Change User Passwords on the Fly

Four Hundred Guru

BACK ISSUES




 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2009 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement