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

    • 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

    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

    An Open Letter To IBM From A Developer Technology Refresh Highlighted By Development Languages And Native Flash Storage

    Leave a Reply Cancel 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

  • Public Preview For Watson Code Assistant for i Available Soon
  • COMMON Youth Movement Continues at POWERUp 2025
  • IBM Preserves Memory Investments Across Power10 And Power11
  • Eradani Uses AI For New EDI And API Service
  • Picking Apart IBM’s $150 Billion In US Manufacturing And R&D
  • FAX/400 And CICS For i Are Dead. What Will IBM Kill Next?
  • Fresche Overhauls X-Analysis With Web UI, AI Smarts
  • Is It Time To Add The Rust Programming Language To IBM i?
  • Is IBM Going To Raise Prices On Power10 Expert Care?
  • IBM i PTF Guide, Volume 27, Number 20

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