• 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
    VISUAL LANSA 16 WEBINAR

    Trying to balance stability and agility in your IBM i environment?

    Join this webinar and explore Visual LANSA 16 – our enhanced professional low-code platform designed to help organizations running on IBM i evolve seamlessly for what’s next.

    🎙️VISUAL LANSA 16 WEBINAR

    Break Monolithic IBM i Applications and Unlock New Value

    Explore modernization without rewriting. Decouple monolithic applications and extend their value through integration with modern services, web frameworks, and cloud technologies.

    🗓️ July 10, 2025

    ⏰ 9 AM – 10 AM CDT (4 PM to 5 PM CEST)

    See the webinar schedule in your time zone

    Register to join the webinar now

    What to Expect

    • Get to know Visual LANSA 16, its core features, latest enhancements, and use cases
    • Understand how you can transition to a MACH-aligned architecture to enable faster innovation
    • Discover native REST APIs, WebView2 support, cloud-ready Azure licensing, and more to help transform and scale your IBM i applications

    Read more about V16 here.

    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

  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools

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