• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Comparing RPG and SQL Functionality

    July 22, 2009 Hey, Mike

    Is there a book on how RPG operation codes and built-in functions translate to SQL? I’m trying to leverage my RPG coding skills to write logic in a DB2 stored procedure. That is, I know exactly what to do in RPG, but I don’t know the SQL equivalent.

    To determine the length of a city name, I would use RPG code like this:

    Eval CityLength = %Len(%Trim(City))
    

    How would I do the same in a stored procedure SET statement? I have used the following to find a field length, but my field is an input parameter to the stored procedure.

    SELECT length(trim(bsrequser)) from bs2hist
    

    Thanks for any help you can give.

    –Doc

    Hey, Doc:

    You’re closer than you think. The SET statement in a DB2 stored procedure is almost identical to the RPG EVAL statement. Here is an example of how to do the operation using SET:

    Create Procedure MyProcedure
    (@City IN Char(60))
    Language SQL
    Begin
        Declare @CityLength Int Not Null Default 0;
    
        Set @CityLength=Length(Trim(@City));
    
    .....
    

    For your information, you can also include a variable (including a parameter variable) in a SELECT column list, if you want to do so (although I use SET most of the time):

    SELECT Length(Trim(@City))
      Into @CityLength 
      From SYSIBM/SYSDUMMY1;
    

    While I don’t know of a specific book to help you translate directly from RPG to SQL, it is fairly simple to move from free-form RPG to SQL just by studying the manuals:

    • RPG Reference
    • SQL Reference

    For instance, in my “glory days” of RPG programming, I would study the list of functions and op-codes to know as many capabilities of the language as possible. Then, when I moved into the SQL world, I did the same thing and eventually noticed many similarities. If you study the list of built-in functions in RPG, you can find equivalents for many of them by reviewing the built-in scalar functions in the SQL manual.

    The tables below show a quick cross-reference between RPG and SQL. Table 1 contains a list of often used RPG built-in functions and the approximate SQL equivalent function or functions. Table 2 shows common RPG operation codes (op-codes) and possible equivalent SQL statements or functions.

    Keep in mind that just because there isn’t a direct equivalent between the languages doesn’t mean that SQL can’t accomplish the same thing. It’s often just a matter of learning to think differently. Nowhere is this “thinking difference” more apparent between RPG and SQL than thinking about working with individual records in RPG and thinking in terms of sets of rows in SQL.

    Sometimes it may take two or more steps in one language over the other. Sometimes, SQL is just lacking ability to work with things like printer files and data areas. But don’t forget SQL can use the strengths of RPG by accessing RPG logic as a function or stored procedure.

    In the end it just takes practice, but the productivity reward for mastering SQL is worth the effort.

    –Michael Sansoterra

    RPG Built-In Function

     

    SQL Built-In Function(s) (or rough equivalent)

    %ABS – Absolute Value of
    Expression

    ABS

     

     

    %CHAR – Convert to
    Character Data

    CAST, CHAR, VARCHAR

     

     

    %DATE – Convert to Date

    CAST, DATE

     

     

    %DAYS – Number of Days

    DAYS (duration)

     

     

    %DEC – Convert to Packed
    Decimal Format

    CAST, DECIMAL

     

     

    %DECH – Convert to Packed
    Decimal Format with Half Adjust

    DECIMAL and ROUND

     

     

    %DIFF – Difference Between
    Two Date, Time, or Timestamp Values

    TIMESTAMPDIFF

     

     

    %DIV – Return Integer
    Portion of Quotient

    Combination of INT
    functions and division using / symbol

     

     

    %FLOAT – Convert to
    Floating Format

    CAST, FLOAT, DOUBLE, REAL

     

     

    %GRAPH – Convert to
    Graphic Value

    CAST, GRAPHIC, VARGRAPHIC

     

     

    %HOURS – Number of Hours

    HOURS

     

     

    %INT – Convert to Integer
    Format

    CAST, INT

     

     

    %INTH – Convert to Integer
    Format with Half Adjust

    INT and ROUND

     

     

    %LEN – Get or Set Length

    LENGTH  (get only)

     

     

    %MINUTES – Number of
    Minutes

    MINUTES (Duration)

     

     

    %MONTHS – Number of Months

    MONTHS (Duration)

     

     

    %MSECONDS – Number of
    Microseconds

    MICROSECOND (Duration)

     

     

    %NULLIND – Query or Set
    Null Indicator

    IS NULL (query only), SET var=NULL

     

     

    %REM – Return Integer
    Remainder

    MOD

     

     

    %REPLACE – Replace
    Character String

    REPLACE (with some
    caveats)

     

     

    %SCAN – Scan for
    Characters

    POSITION, POSSTR

     

     

    %SECONDS – Number of
    Seconds

    SECONDS (Duration)

     

     

    %SQRT – Square Root of Expression

    SQRT, POWER

     

     

    %SUBDT – Extract a Portion
    of a Date, Time, or Timestamp

    DAY, YEAR, MONTH, HOUR,
    MINUTE, etc.

     

     

    %SUBST – Get Substring

    SUBSTR

     

     

    %TIME – Convert to Time

    CAST, TIME

     

     

    %TIMESTAMP – Convert to
    Timestamp

    CAST, TIMESTAMP

     

     

    %TRIM – Trim Blanks at
    Edges

    TRIM, STRIP

     

     

    %TRIML – Trim Leading
    Blanks

    LTRIM

     

     

    %TRIMR – Trim Trailing
    Blanks

    RTRIM

     

     

    %UCS2 – Convert to UCS-2
    Value

    CAST(x AS VARGRAPHIC(n)
    CCSID 13488)

     

     

    %XLATE – Translate

    TRANSLATE

     

     

    %YEARS – Number of Years

    YEARS

    Table 1: Comparison of RPG built-in functions and possible SQL substitutes.

    RPG Op-Code

    Possible SQL Replacement

    ADD – Add

    + operator

     

     

    ADDDUR – Add Duration

    Date or Timestamp +
    duration value

     

     

    CALL – Call a program

    CALL

     

     

    CALLP – Call a prototyped
    procedure or program

    Create Procedure
    (External) / CALL

     

     

    CAT – Concatenate two
    strings

    || operator, CONCAT
    function

     

     

    CHAIN – Random retrieval
    from a file

    SELECT INTO, SELECT …
    FETCH FIRST 1 ROW ONLY

     

     

    COMMIT – Commit

    COMMIT

     

     

    COMP – Compare

    =, <=, <, >,
    >=, <>

     

     

    DELETE – Delete Record

    DELETE statement with
    WHERE criteria or positioned delete using a cursor

     

     

    DIV – Divide

    / operator

     

     

    DOxxx – Do

    WHILE, REPEAT

     

     

    EVAL – Evaluate expression

    SET

     

     

    GOTO – Go to

    GOTO

     

     

    IF, ELSE

    IF THEN, ELSE

     

     

    ITER – Iterate

    ITERATE

     

     

    MONITOR, ON-ERROR – Begin
    a monitor group

    DECLARE xxxx HANDLER

     

     

    MOVE, MOVEL, EVALR – Move,
    Move Left, etc.

    Combination of SET and
    built-in functions

     

     

    MULT – Multiply

    * operator

     

     

    MVR – Move remainder

    MOD function

     

     

    READ, READP – Read, Read
    Prior

    DECLARE CURSOR, SELECT and
    FETCH NEXT or FETCH PRIOR

     

     

    READE, READPE – Read
    Equal, Read Prior Equal

    DECLARE CURSOR, SELECT and
    FETCH NEXT or FETCH PRIOR with appropriate WHERE criteria

     

     

    ROLBK – Roll back

    ROLLBACK

     

     

    SCAN – Scan String

    POSSTR built-in function

     

     

    SELECT, OTHER – Begin a
    Select Group

    CASE

     

     

    SUBDUR – Subtract duration

    Date or Timestamp –
    duration value

     

     

    TIME – Retrieve time and
    date

    Current_Date, Current_Time, Current_Timestamp registers

     

     

    UPDATE – Modify an
    existing record

    UPDATE statement with
    WHERE criteria or positioned update using a cursor

     

     

    WRITE – Write a record

    INSERT INTO

     

     

    XLATE – Translate

    TRANSLATE function

    Table 2: Comparison of RPG operation codes and possible SQL substitutes.



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

    Sponsored Links

    Profound Logic Software:  Tune in to Profound Logic TV for FREE educational videos and tips
    Maximum Availability:  *noMAX - Subscription edition now available (US & UK)
    COMMON:  Celebrate our 50th anniversary at annual conference, May 2 - 6, 2010, in Orlando

    IT Jungle Store Top Book Picks

    Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
    The iSeries Express Web Implementer's Guide: List Price, $49.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 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
    Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
    Getting Started with WebSphere Express for iSeries: List Price, $49.00
    Can the AS/400 Survive IBM?: List Price, $49.00
    Chip Wars: List Price, $29.95

    AIG-Israel Taps Raz-Lee for i OS Security Tool Power 7: Lots of Cores, Lots of Threads

    Leave a Reply Cancel reply

Volume 9, Number 24 -- July 22, 2009
THIS ISSUE SPONSORED BY:

Help/Systems
WorksRight Software
Guild Companies

Table of Contents

  • Circumventing Two Limitations of CPYTOIMPF
  • Comparing RPG and SQL Functionality
  • Admin Alert: Treating IFS Objects Like Stream File Objects

Content archive

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

Recent Posts

  • Liam Allan Shares What’s Coming Next With Code For IBM i
  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • IBM i PTF Guide, Volume 27, Number 25
  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24

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