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

    May 5, 2015 Hey, Ted

    I am trying to use your FMTDATE SQL function but the system tells me it can’t find it. I know FMTDATE is there. Can you help?

    –Robert

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

    I’ve received a compliment or two (at least) for the FMTDATE (format date) function I published back in 2009. (Can it really have been so long? How time flies!) I use it heavily, and I’m not the only one. If you’re not familiar with FMTDATE, please read Formatting Dates with SQL before continuing.

    To address Robert’s question, the reason the system said that it could not find the function was that Robert was passing a character argument to the first parameter. I wrote the FMTDATE function to accept a numeric value in the first parameter. One might think that the system would respond that the first parameter was of the wrong data type, but not so. Here’s why: SQL allows two or more functions to have the same name as long as the parameter lists differ.

    This phenomenon is known as function overloading, and it’s marvelous. When the system must run a function, it looks for a function of the correct name that has a compatible parameter list. “Compatible” means:

    1. The number of parameters must equal the number of supplied arguments

    2. The system must be able to convert each argument to the data type of the corresponding parameter in the function definition

    To help Robert with his problem was a simple matter of overloading the FMTDATE function. In a matter of minutes, I made two changes of significance to my RPG source code.

    1. I renamed the FMTDATE subprocedure to FMTDATEN. This was not necessary. I added the N to emphasize that the subprocedure was for numeric data. (I like clarity.)

    2. I added a subprocedure called FMTDATEA, where A stands for alphanumeric. FMTDATE converts the character value to numeric and calls FMTDATEN. Here’s that subprocedure in its entirety.

    P FmtDateA        b                   export
    D                 pi            10a   varying
    D   inDate                       8a   varying const
    D   inFromFmt                    8a   varying const
    D   inToFmt                      8a   varying const 
    D   inNullArray                  6a   const
    D   ouNull                       5i 0
    
     /free
         monitor;
            return FmtDateN (%dec(inDate:8:0): inFromFmt:
                             inToFmt: inNullArray: ouNull);
         on-error;
            ouNull = gNullValue;
            return *blank;
         endmon;
    
     /end-free
    P                 e
    

    After creating the module and service program, I created two (yes, two!) FMTDATE functions.

    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(FMTDATEN)'
    
    create function xxx/fmtdate
    (inDate varchar(8), 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(FMTDATEA)'
    

    Two functions of the same name run two different subprocedures in the FMTDATE service program. Is that fine, or what?

    One word of warning: If you’re using FMTDATE within programs, as I do, you will have to recompile those programs because of the change to the procedure exports in the service program.

    Now, if only IBM would allow overloading of subprocedures in RPG! I could do some powerful if-less programming!

    RELATED STORIES

    Formatting Dates with SQL

    UDF Concepts (IBM Knowledge Center)

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Tags:

    Sponsored by
    Raz-Lee Security

    The MFA Mobile App provides a secure and user-friendly way to add strong authentication without complicating access. It enables users to approve login requests or generate one-time passwords directly from their mobile device, ensuring that access is granted only after a second, trusted factor is verified.

    Learn More

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Sponsored Links

    Remain Software:  Take control of your software modernization process with TD/OMS and X-Analysis
    ProData Computer Services:  SQL/Pro 5.0 - New & Improved! Download today!
    United Computer Group:  VAULT400 BaaS delivers secure cloud backup and DR solutions

    Pipelined User-Defined Table Functions In DB2 For i Ruby And Existing Databases

    Leave a ReplyCancel reply

Volume 15, Number 09 -- May 5, 2015
THIS ISSUE SPONSORED BY:

WorksRight Software
PowerTech
United Computer Group, Inc.

Table of Contents

  • Pipelined User-Defined Table Functions In DB2 For i
  • Formatting Dates with SQL, Take 2
  • Ruby And Existing Databases

Content archive

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

Recent Posts

  • Inside The Encryption Key Management Changes In IBM i 7.6
  • FalconStor Moved To The Blue Lagoon, And Is Poised For Growth Because Of It
  • Guru: Claude’s SQL Tip
  • Astera Makes Extracting Legacy Report Data an AI Specialty
  • IBM i PTF Guide, Volume 28, Number 27
  • Welcoming The New IBM i Chief Architect And Other New Top Brass
  • A Deep Dive Into That Power S1112 Entry Power11 Server
  • Guru: Beyond Three-Part Naming – Running SQL Across Remote IBM i Systems
  • How IBM Bolstered IBM i Resilience In The Summer Tech Refreshes
  • IBM i PTF Guide, Volume 28, Number 26

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