• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Fuzzy Matching In RPG

    December 3, 2014 Ted Holt

    Note: The code accompanying this article is available for download here.

    SQL allows you to use wildcard characters with the LIKE operator to search a column for a pattern. As they say in the GEICO commercials, “Everybody knows that.” Well, did you know you can do the same thing in RPG programs?

    If you’re not familiar with LIKE, read about it here. You can use the LIKE operator to find data with inexact matches. For instance, find all customers whose names contain “ACME”.

    select * from cust
     where name like '%ACME%'
    
       ID  NAME
    =====  ==========================================
    19883  ACME Amalgamated Widget and Doodad Company
    58842  ACME Glass Home Builders, LLC
    

    If you need a case-insensitive search, use the UPPER (or UCASE) function.

    select * from cust
     where upper(name) like '%ACME%'
    
       ID  NAME
    =====  ==========================================
    19883  ACME Amalgamated Widget and Doodad Company
    39003  Pete Slacmen Tricycle Supplies, Inc.
    58842  ACME Glass Home Builders, LLC
    

    Another thing everybody knows is that we can use SQL features in RPG programs. Subprocedure MATCHES uses LIKE with RPG host variables.

    dcl-proc  Matches;
    
       dcl-pi Matches  ind;
          inSource     like(String)  const  options(*trim);
          inPattern    like(String)  const  options(*trim);
       end-pi;
    
       dcl-s  Matched  char(1);
    
       // Try for an exact match
       if inSource = inPattern;
          return *on;
       endif;
    
       // Try for a wild-card match
       exec sql
          set :Matched = case
             when :inSource like :inPattern
                then '1' else '0' end;
       if Matched = '1';
          return *on;
       endif;
    
       // Otherwise, no match.
       return *off;
    
    end-proc Matches;
    

    Here it is in partial fixed-format code.

    P Matches         b
    
    D Matches         pi              n
    D   inSource                          like(String) const options(*trim)
    D   inPattern                         like(String) const options(*trim)
    
    D Matched         s              1a
    
     /free
         // Try for an exact match
         if inSource = inPattern;
            return *on;
         endif;
    
         // Try for a wild-card match
         exec sql
            set :Matched = case
               when :inSource like :inPattern
                  then '1' else '0' end;
         if Matched = '1';
            return *on;
         endif;
    
         // Otherwise, no match.
         return *off;
     /end-free
    P Matches         e
    

    MATCHES first attempts an exact match of two strings. If that doesn’t work, it tries the LIKE operator. The following RPG source code assigns a value of *ON to indicator FOUND.

    SearchName = 'ACME%Widget%Corporation';
    CustomerName = 'ACME Amalgamated Widget and Doodad Corporation';
    
    Found = Matches(CustomerName: SearchName);
    

    I forget where I learned this technique, but I think it was from something Birgitta Hauser posted on the Web. (Birgitta’s truly a guru, but everybody knows that.) To whoever it was, I say, “many thanks!” I have production code that runs all day long and uses this technique.

    RELATED REFERENCE

    LIKE Predicate, IBM i 7.1 Knowledge Center



                         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
    DRV Tech

    Get More Out of Your IBM i

    With soaring costs, operational data is more critical than ever. IBM shops need faster, easier ways to distribute IBM applications-based data to users more efficiently, no matter where they are.

    The Problem:

    For Users, IBM Data Can Be Difficult to Get To

    IBM Applications generate reports as spooled files, originally designed to be printed. Often those reports are packed together with so much data it makes them difficult to read. Add to that hardcopy is a pain to distribute. User-friendly formats like Excel and PDF are better, offering sorting, searching, and easy portability but getting IBM reports into these formats can be tricky without the right tools.

    The Solution:

    IBM i Reports can easily be converted to easy to read and share formats like Excel and PDF and Delivered by Email

    Converting IBM i, iSeries, and AS400 reports into Excel and PDF is now a lot easier with SpoolFlex software by DRV Tech.  If you or your users are still doing this manually, think how much time is wasted dragging and reformatting to make a report readable. How much time would be saved if they were automatically formatted correctly and delivered to one or multiple recipients.

    SpoolFlex converts spooled files to Excel and PDF, automatically emailing them, and saving copies to network shared folders. SpoolFlex converts complex reports to Excel, removing unwanted headers, splitting large reports out for individual recipients, and delivering to users whether they are at the office or working from home.

    Watch our 2-minute video and see DRV’s powerful SpoolFlex software can solve your file conversion challenges.

    Watch Video

    DRV Tech

    www.drvtech.com

    866.378.3366

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Cilasoft:  Stay on top of your most difficult IBM i security challenges with our Auditing and Security Suite.
    Profound Logic Software:  Now On-Demand Webinar: "See What i Can Do with Mobile Applications"
    BCD:  Beat Your Q4 Numbers with Real-Time Analytics on IBM i. Watch video!

    More IT Jungle Resources:

    System i PTF Guide: Weekly PTF Updates
    IBM i Events Calendar: National Conferences, Local Events, and Webinars
    Breaking News: News Hot Off The Press
    TPM @ EnterpriseTech: High Performance Computing Industry News From ITJ EIC Timothy Prickett Morgan

    Web Dev Tool from mrc Goes ‘Responsive’ Entry Power8 Systems Get Express Pricing, Fat Memory

    Leave a Reply Cancel reply

Volume 14, Number 26 -- December 3, 2014
THIS ISSUE SPONSORED BY:

Focal Point Solutions Group
WorksRight Software
Shield Advanced Solutions

Table of Contents

  • Fuzzy Matching In RPG
  • TR8 DB2 For i Enhancements, Part 2
  • Admin Alert: What Should An IBM i Administrator Do, Part 1

Content archive

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

Recent Posts

  • The Power11 Transistor Count Discrepancies Explained – Sort Of
  • Is Your IBM i HA/DR Actually Tested – Or Just Installed?
  • Big Blue Delivers IBM i Customer Requests In ACS Update
  • New DbToo SDK Hooks RPG And Db2 For i To External Services
  • IBM i PTF Guide, Volume 27, Number 33
  • Tool Aims To Streamline Git Integration For Old School IBM i Devs
  • IBM To Add Full System Replication And FlashCopy To PowerHA
  • Guru: Decoding Base64 ASCII
  • The Price Tweaking Continues For Power Systems
  • IBM i PTF Guide, Volume 27, Numbers 31 And 32

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