Midrange News for the IBM i Community

Posted by: Bob Cozzi
Rogue Programmer
Cozzi Productions, Inc.
Quick Scan and Ignore Case in RPG IV
has no ratings.
Published: 27 Dec 2011
Revised: 23 Jan 2013 - 4055 days ago
Last viewed on: 01 Mar 2024 (12156 views) 

Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.

Quick Scan and Ignore Case in RPG IV Published by: Bob Cozzi on 27 Dec 2011 view comments

Too bad RPG IV still doesn't support a case-insensitive scan operator. This is so painful to RPG developers that I sadly see them building applications that force end-users to type in data in ALL UPPER CASE to simply avoid the short-coming in the %SCAN built-in function.

What some developers forget, is that you can embed existing RPG IV built-in functions as a parameter of another RPG IV built-in function. For example, you can embed %XLATE as a parameter of the %SCAN built-in function, as follows:

     D UP              C                   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'          
     D low             C                   'abcdefghijklmnopqrstuvwxyz'          
     D desc            S           4096A   Varying Inz('john q public')          
     D nPos            S             10I 0                                       
          nPos = %scan('Q' : %xlate(low:up:DESC));                               

 In this example, I've set up two named constants, UP and LOW. The used those as the translation table for %XLATE. I further embedded that %XLATE into a %SCAN built-in function while scanning the DESC field for the letter "Q". In this case, since the "Q" is in upper case, I %xlate the data I am scanning to uppercase. This allows me to let the end-users type in data as mixed case, and not worry about how the search is going to work.

In addition, both SQL and legacy DDS Logical Files have the ability to dynamically translate their search data to uppercase.

  • TRNTBL(QSYSTRNTBL) - Use this in logical file DDS to convert the logical's key fields to all upper case.
  • upper( myfield ) - Use this in SQL to convert of field (from the database file or RPG IV program) to upper case. There is also a LOWER( x ) built-in function in SQL that obviously, converts to lower case.

Return to midrangenews.com home page.
Sort Ascend | Descend