• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: When %SCAN Isn’t Sufficient

    June 24, 2019 Ted Holt

    The RPG %SCAN built-in function is wonderful! I can still remember having to look for a string within a string using RPG II on System/36. What an ordeal that was! Yet in some situations %SCAN can’t do all I need it to do. In those cases, I rely on the power of SQL.

    One case where SQL comes in handy is when I need a case-insensitive scan. Instead of RPG’s %SCAN function, I use SQL’s LOCATE and UPPER functions, like this:

    dcl-s  Description  char(48);
    dcl-s  pos          int (10);
    
    exec sql
       set :pos = locate ('HAMMER', upper(:Description));
    

    If Description has the value Claw hammer 16oz, pos takes the value of 6.

    Two similar SQL functions that you may want to read about are POSITION and POSSTR.

    Another situation is with wild card scans. You can use SQL’s LIKE predicate for those.

    dcl-s  Description  char(48);
    dcl-s  Matched      char( 1);
    
    exec sql
       set :Matched = case
          when :Description like '%16%oz%hammer%'
             then '1' else '0' end;
    

    This code determines whether Description refers to a 16-ounce hammer. Here are some values that cause Matched to be set to 1.

    • 16oz claw hammer
    • 16-oz. claw hammer
    • 16 oz claw hammer
    • Famousbrand 16 oz claw hammer
    • 16 oz bricklayers hammer

    You can also scan for regular expressions. Use the REGEXP_LIKE predicate.

    dcl-s  Description  char(48);
    dcl-s  Matched      char( 1);
    
    exec sql
       set :Matched = case
          when regexp_like (:Description, '16.*oz.*(hammer|mallet)')
             then '1' else '0' end;
    

    This code determines whether Description refers to a 16-ounce hammer or mallet. Any of the values from the previous example cause Matched to be set to 1. Here are a couple more:

    • 16oz rubber mallet
    • Famousbrand 16 oz mallet

    If you want the scan to ignore case, put an i in the third parameter of REGEXP_LIKE.

    dcl-s  Description  char(48);
    dcl-s  Matched      char( 1);
    
    exec sql
       set :Matched = case
          when regexp_like (:Description, '16.*oz.*(hammer|mallet)', 'i')
             then '1' else '0' end;
    

    You can use the REGEXP_INSTR function instead of the REGEXP_LIKE predicate.

    dcl-s  Description  char(48);                                     
    dcl-s  pos          int (10);                                     
                                                                      
    exec sql                                                          
       set :pos =                                                     
               regexp_instr (:Description, '16.*oz.*(hammer|mallet)');
    

    If Description is Famousbrand 16oz claw hammer, pos is 13.

    I really, really, really appreciate all IBM has done to give us wonderful functions like %SCAN in RPG. I also really, really, really appreciate their making SQL available to use in RPG programs. We’ve never had it so good.

    RELATED STORIES

    Native Regular Expressions In DB2 For i 7.1 And 7.2

    Fuzzy Matching in RPG

    LOCATE

    REGEXP_INSTR

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, Four Hundred Guru, Guru, IBM i, RPG, SQL, System/36

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Four Hundred Monitor, June 24 ARCAD Brings Traditional 5250 Development Into DevOps Fold

    One thought on “Guru: When %SCAN Isn’t Sufficient”

    • Ken says:
      June 24, 2019 at 9:32 am

      Hello Ted,

      Thanks for the tip. I did not know you could use like to check for multiple values. That is really cool.

      Ken

      Reply

    Leave a Reply Cancel reply

TFH Volume: 29 Issue: 39

This Issue Sponsored By

  • Maxava
  • ProData Computer Services
  • RPG & DB2 Summit
  • iTech Solutions
  • T.L. Ashford

Table of Contents

  • How Big Blue Stacks Up IBM i On Premises And On Cloud
  • ARCAD Brings Traditional 5250 Development Into DevOps Fold
  • Guru: When %SCAN Isn’t Sufficient
  • Four Hundred Monitor, June 24
  • IBM i PTF Guide, Volume 21, Number 25

Content archive

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

Recent Posts

  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23
  • SEU’s Fate, An IBM i V8, And The Odds Of A Power13
  • Tandberg Bankruptcy Leaves A Hole In IBM Power Storage
  • RPG Code Generation And The Agentic Future Of IBM i
  • A Bunch Of IBM i-Power Systems Things To Be Aware Of
  • IBM i PTF Guide, Volume 27, Numbers 21 And 22

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