IBMi (AS400) fans only : Reverse date 6 0 in one instruction


#IBMiSample


In this example, the OTDTP0 field is numeric 6 with 0 decimal places.


It should contain a number rapresenting a DATE in yymmdd (i.e 210208 - February 8, 2021) or ddmmyy format  (i.e 080221 - February 8, 2021).

The following instructions turns yymmdd to ddmmyy or viceversa (the other way around).


OTDTP0 zoned(6);

OTDTP0 = %dec(%char(%date(OTDTP0:*ymd):*dmy0):6:0); // yymmdd to ddmmyy

OTDTP0 = %dec(%date(OTDTP0:*ymd):*dmy); // yymmdd to ddmmyy


I appreciate all the comments made on this blog.

Comments

  1. //-- %char is not necesssary, mmddyy to yymmdd:

    YYMMDD = %Dec(%date(MMDDYY:*mdy):*ymd);

    ReplyDelete
    Replies
    1. Thanks for your help. I updated the post according to your suggestion

      Delete
  2. I find using a monitor/on-error on the conversion line catches invalid dates better than checking for not zero.

    ReplyDelete
  3. Instead of qualifying that your decimal field <> *zeros, if you monitor on the conversion, you can set the value to zero, or a date you can identify as a problem without the program bombing. (Monitor/conversion/on-error/eval to zero/endmon)

    ReplyDelete

Post a Comment

Popular posts from this blog

(IBM i fans only) Efficient WRKSPLF with WSF - How to Search string into spooled files, Sort, and Generate PDFs on IBMi

(IBM i fans only) Detecting and Handling Non-Printable Characters in DB2 SQL Using LOCATE() and REPLACE() Functions

(IBM i fans only) How to Sniff User Access