fhg
Volume 9, Number 24 -- July 22, 2009

Comparing RPG and SQL Functionality

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

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


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
E-mail: software@worksright.com
Web site: www.worksright.com


Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Erwin Earley, Brian Kelly, Michael Sansoterra
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

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


 
The Four Hundred
Sundry Power Systems i Storage Announcements

Servers Slammed in IBM's Second Quarter

IBM Sunsets More Power Systems Features

As I See It: Injured Wing

Fincham Rides Point for iManifest EMEA

Four Hundred Stuff
IBM Kills Secure Perspectives Tool

Bicycle Seller Rolls with Electronic Vaulting Backup and DR from UCG

JDA Shares Plans for E3 Fulfillment Products

Quadrant Adds a Slew of Updates to Forms Software

Kisco Gives i OS Auditing Tool a Web Makeover

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

System i PTF Guide
July 18, 2009: Volume 11, Number 29

July 11, 2009: Volume 11, Number 28

July 4, 2009: Volume 11, Number 27

June 27, 2009: Volume 11, Number 26

June 20, 2009: Volume 11, Number 25

June 13, 2009: Volume 11, Number 24

TPM at The Register
Intel slashes prices on desktop, server chips

Dell partner CIT dodges bankruptcy

IBM peddles FCoE switches from Brocade and, yes, Cisco

HP chases Sun Oracle server shops

IBM: Revenues down, profits up in Q2

Sun shareholders approve Oracle deal

PCs do better than expected in Q2

The curious case of Sun's hardware biz

Bull to do homegrown Nehalem EX chipset

Nehalem and Atom save Intel's Q2 cookies

Microsoft hosts Feynman lecture series

Supers get greener

IBM drops Istanbuls into big Opteron box

Sun: Q4 sales to drop by a third, sees deeper losses

THIS ISSUE SPONSORED BY:

Help/Systems
WorksRight Software
Guild Companies


Printer Friendly Version


TABLE OF CONTENTS
Circumventing Two Limitations of CPYTOIMPF

Comparing RPG and SQL Functionality

Admin Alert: Treating IFS Objects Like Stream File Objects

Four Hundred Guru

BACK ISSUES




 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2009 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement