• 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
    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

    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

  • 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