• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • End-of-Year Odds and Ends

    December 17, 2008 Timothy Prickett Morgan

    Merry Christmas, intelligent i professional! Let’s end the year with a few little tips, including two unusual ways to use OPNQRYF’s %ABSVAL function from reader Victor Pisman. May 2009 be the best year ever for all of us.

    –Ted

    Run Qshell from CL Procedures

    Q: Do you have a simple example of using Qshell with a script? I think I can muddle through setting up the script. I am unsure of how to call it from a CL procedure.

    A: You can use the dot utility to run a script that you’ve created in the IFS. This command runs the IFS file MyScript.sh.

    SBMJOB CMD(QSH CMD('. MyScript.sh')) JOB(MYJOB) JOBQ(SOMEJOBQ)
    

    Or you can put the script into a CL variable and execute it from there, like this:

          chgvar  &Script +  
            (+               
            'cd' *bcat &CurDir *bcat '|| exit 1; + 
             [[ -d text ]] || exit 3; + 
             [[ -d archive ]] || exit 5; +
             ... more script lines omitted in this example ...
             ')
          qsh cmd(&Script)
    

    Please Find My Message

    Q: How can I search through the descriptions in a message file without manually looking at all the messages?

    A: Print the message descriptions.

    DSPMSGD RANGE(*FIRST *LAST) MSGF(MYMSGF) DETAIL(*BASIC) OUTPUT(*PRINT)
    

    Then look at the spool file and search.

    Everything Is Oojah-cum-spiff

    Q: How do I display a message to a user at the bottom of the screen after a CL program has completed?

    A: Use SNDPGMMSG to send a completion message.

    sndpgmmsg  msgid(cpf9898) msgf(qcpfmsg) msgtype(*comp) +
                  msgdta('Program' *bcat &PgmName *bcat +
                         'completed normally')
    

    Notice the message type is *COMP, for completion.

    DATFMT Error in SQL

    Q: I am having a problem with date data type fields in my files. If the date fields are the default “0001-01-01”, SQL gives me a mapping error. If I put the DATFMT(*ISO) keyword on the HSPEC, it still doesn’t fix the problem. I have to prompt the compile and change the CRTSQLRPGI DATFMT to be *ISO to fix the problem. Have you had this problem?

    A: The compiler wants to use the default *MDY format, which only accepts dates from 1940-2039. Prompting and changing the format makes it use *ISO format instead. If you don’t want to have to prompt, put the compiler option in your source code.

    exec sql set option datfmt=*iso;
    

    Remember that options have to precede all other SQL commands in the program.

    You Can Look, But Don’t Touch!

    Q: I want to have users be able to view a job queue to see that their program is running or waiting to run. I don’t want them to be able to change any attributes or have access to the command line. Is this possible?

    A: Revoke authority to commands ADDJOBQE, CHGJOBQE and RMVJOBQE.

    Use %ABSVAL to Avoid Division by Zero

    We know that division by zero is undefined. But sometimes we are faced with that possibility in real life. For example, a profit margin value is calculated by the following formula.

    Margin = (Price - Cost) / Price
    

    Normally if the price is zero, this formula cannot be used. But with %ABSVAL this formula can be used even if the price is zero. The following margin formula handles division by zero.

    Margin = (Price-Cost) / ((%ABSVAL (Price - 0.01)+Price+0.01)/2)
    

    For example, if price = 9.60 and cost = 7.20 the formula returns a margin of 25 percent.

    Margin = (9.60-7.20) / ((%ABSVAL (9.60-0.01)+9.60+0.01)/2) 
           = 2.40 / ((9.59 + 9.61)/2)
           = 2.40 / 9.60
           = 0.25.	
    

    When the price and the cost are zero, the margin is zero.

    Margin = (0-0) / ((%ABSVAL (0-0.01)+0+0.01)/2) = 0 / (0.01 + 0.01)/2
           = 0 / 0.01
           = 0 
    

    Here’s a CL example showing how the formula would be coded.

    OPNQRYF FILE ((PRR)) FORMAT (TESTF3) KEYFLD ((PRITNO)) + 
       MAPFLD ((FLD1 'PRICE - 0.01')  +       
               (FLD2 'PRICE + 0.01')   +                    
               (FLD3 '(%ABSVAL (FLD1)+(FLD2))*0.5')   +       
               (FLD4  '(PRICE - COST) * 100')   +  
               (MARGIN  'FLD4 / FLD3')) +
       OPNID (QRYFILE)                      
    

    Use %ABSVAL to Convert Negative Numbers to Zero

    The quantity available for purchase for an item is defined as the quantity on hand less the current customer requirements. If there are 100 units of an item on hand, and there are customer requirements for 80 of them, 20 are available. But if there are current requirements for 150, then the available quantity is 100-150, or -50.

    The negative values are equivalent to zero and needs to be converted to zero. The following is the formula to convert negative values to zero using %ABSVAL:

    Available =  (%ABSVAL (OnHand - Requirements) + OnHand - Requirements) / 2
    

    If OnHand is 100 and Requirements is 80, Available is 20.

    Available = (%ABSVAL (100 - 80) + 100 - 80) / 2
              = (20 + 20) / 2
              = 20
    

    And when OnHand is 100 and Requirements is 150, Available is zero.

    Available = (%ABSVAL (100 - 150) + 100 - 150) / 2
              = (50 - 50) / 2
              = 0
    

    The following CL demonstrates this formula:

    OPNQRYF FILE ((IBR)) FORMAT (TESTF1) KEYFLD ((ITEM)) +
       MAPFLD((FLD2 'ONHAND - REQ')  +   
              (FLD1 '(%ABSVAL(FLD2)+FLD2)*0.5')) 
    



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    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

    PowerTech:  Incorporating real-time security events from the System i into a security program
    Safedata:  FREE White Paper - IBM iSeries Recovery Options: An Executive Guide
    COMMON:  Join us at the 2009 annual meeting and expo, April 26-30, Reno, Nevada

    IT Jungle Store Top Book Picks

    Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
    Getting Started with PHP for i5/OS: List Price, $59.95
    The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
    The System i Pocket RPG & RPG IV Guide: List Price, $69.95
    The iSeries Pocket Database Guide: List Price, $59.00
    The iSeries Pocket Developers' Guide: List Price, $59.00
    The iSeries Pocket SQL Guide: List Price, $59.00
    The iSeries Pocket Query Guide: List Price, $49.00
    The iSeries Pocket WebFacing Primer: List Price, $39.00
    Migrating to WebSphere Express for iSeries: List Price, $49.00
    iSeries Express Web Implementer's Guide: List Price, $59.00
    Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
    Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
    Getting Started with WebSphere Express for iSeries: List Price, $49.00
    WebFacing Application Design and Development Guide: List Price, $55.00
    Can the AS/400 Survive IBM?: List Price, $49.00
    The All-Everything Machine: List Price, $29.95
    Chip Wars: List Price, $29.95

    IBM Seeks Organic Solution to Power Systems Challenge, Global Warming Now What?

    Leave a Reply Cancel reply

Volume 8, Number 43 -- December 17, 2008
THIS ISSUE SPONSORED BY:

Help/Systems
ProData Computer Services
Group8 Security

Table of Contents

  • Two A-maze-ing Programs
  • End-of-Year Odds and Ends
  • Admin Alert: Upcoming i5/OS and AnyNet End of Service Dates

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