• 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, Take 3

    July 12, 2016 Ted Holt

    This story contains code, which you can download here.

     

    I love it when someone improves something I’ve produced. In this case, that someone was Barry Arnold, an A-1COBOL programmer with whom I had the privilege to work once upon a time. Barry improved my FMTDATE SQL function for use in his shop. Maybe his enhancement will help you, too.

    Barry’s shop, like many others, must deal with data from many different sources, and that data is often formatted in ways that are not conducive to RPG and COBOL programming. Barry found himself having to de-edit character dates before passing them to the FMTDATE function. He reasoned that FMTDATE should be able to handle the de-editing, and I have to agree.

    I modified the character version of FMTDATE in two ways:

    • I increased the size of the date (the first argument) from eight to 10 bytes to allow for edited eight-byte dates.
    • If an editing character is specified in the source format (the second argument), I remove all occurrences of the editing character from the date before attempting the conversion. In the previous version, I ignored an editing character, as I do with the numeric version of FMTDATE.

    Here’s an example:

    SELECT some_date, fmtdate(some_date,'mdy/','iso-')
      FROM mytable
    

     

    SOME_DATE

     

    FMTDATE

     

    02/28/16

     

    2016-02-28

     

    12/25/15

     

    2015-12-25

     

    12 25 15

     

    2015-12-25

     

    12-25-15

     

    null

     

     

    Notice that FMTDATE can handle internal blanks. This was not intentional; it just worked out that way. It also ignores leading and trailing blanks.

    Notice also that FMTDATE does not remove editing characters other than the one specified in the format.

    Thanks to Barry for suggesting this improvement. This would be a pitiful world if I had to think of everything myself.

    Ted Holt welcomes your comments and questions. Email him through the IT Jungle Contacts page.

     

    RELATED STORIES

    Easier Overloading of SQL Functions

    Ease the Burden of Overloading

    Formatting Dates with SQL, Take 2

     

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Maxava

    Migrate IBM i with Confidence

    Tired of costly and risky migrations? Maxava Migrate Live minimizes disruption with seamless transitions. Upgrading to Power10 or cloud hosted system, Maxava has you covered!

    Learn More

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    System i Developer:  RPG & DB2 Summit - October 4-6 2016 in Chicago. Register now! COMMON:  Open Source and Systems Management at the COMMON Forum. August 24-25 in Chicago. OCEAN:  3 days of inspiration! 2016 IBM i Technical Conference, July 21-23, Costa Mesa, California

    Maxava Opens Up Bidding For iFoundation Grants A Second Look At SQL Descriptors

    Leave a Reply Cancel reply

Volume 16, Number 15 -- July 12, 2016
THIS ISSUE SPONSORED BY:

WorksRight Software
Valence Framework for IBM i
System i Developer

Table of Contents

  • A Generic Character Editing Routine
  • Formatting Dates With SQL, Take 3
  • New OLAP Aggregate Functions In DB2 for i, Part 1

Content archive

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

Recent Posts

  • POWERUp 2025 –Your Source For IBM i 7.6 Information
  • Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More
  • Guru: Creating An SQL Stored Procedure That Returns A Result Set
  • As I See It: At Any Cost
  • IBM i PTF Guide, Volume 27, Number 19
  • 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

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