Search code examples
ibm-midrangedb2-400rpgle

How to get the last day of month in a CL


I need to get the last day of the previous month. So this CL can run on a 3rd day of the new month. Can I do a Select statement in the CL (we are in 7.1)

Or if not what are some steps needed to code and save this date as well in a small table.


Solution

  • Here is a simple example of calculating the last day of the previous month using CL.

    PGM                                                                       
    
    DCL        VAR(&CYMD) TYPE(*CHAR) LEN(7)                                  
    DCL        VAR(&JUL) TYPE(*CHAR) LEN(5)                                   
    DCL        VAR(&JUL#) TYPE(*DEC) LEN(5)                                   
    
    RTVJOBA    CYMDDATE(&CYMD)                                                
    CHGVAR     VAR(%SST(&CYMD 6 2)) VALUE('01')                               
    CVTDAT     DATE(&CYMD) TOVAR(&JUL) FROMFMT(*CYMD) TOFMT(*JUL) TOSEP(*NONE)                                     
    CHGVAR     VAR(&JUL#) VALUE(&JUL)                                         
    CHGVAR     VAR(&JUL#) VALUE(&JUL# - 1)                                    
    CHGVAR     VAR(&JUL) VALUE(&JUL#)                                         
    CVTDAT     DATE(&JUL) TOVAR(&CYMD) FROMFMT(*JUL) TOFMT(*CYMD) TOSEP(*NONE)
    
    ENDPGM