• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Formatting Dates with SQL

    August 19, 2009 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

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    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

    Original Software Teams with AppLabs for Software Testing CIOs Say Power Systems Are the Most Reliable

    Leave a Reply Cancel reply

Volume 9, Number 26 -- August 19, 2009
THIS ISSUE SPONSORED BY:

Help/Systems
System i Developer
Twin Data

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

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • IBM Unveils Manzan, A New Open Source Event Monitor For IBM i
  • Say Goodbye To Downtime: Update Your Database Without Taking Your Business Offline
  • i-Rays Brings Observability To IBM i Performance Problems
  • Another Non-TR “Technology Refresh” Happens With IBM i TR6
  • IBM i PTF Guide, Volume 27, Number 18
  • Will The Turbulent Economy Downdraft IBM Systems Or Lift It?
  • How IBM Improved The Database With IBM i 7.6
  • Rocket Celebrates 35th Anniversary As Private Equity Owner Ponders Sale
  • 50 Acres And A Humanoid Robot With An AI Avatar
  • IBM i PTF Guide, Volume 27, Number 17

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle