Home > AS/400 Tips > iSeries programmer tips > Two ways to validate packed decimal data
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ISERIES PROGRAMMER TIPS

Two ways to validate packed decimal data


Nick Hobson
04.03.2002
Rating: -3.70- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Sometimes it is necessary to validate packed decimal data before attempting to use it. For example, you may have received a flat file from another system. In this case, the record positions that are supposed to contain packed data should be validated before being loaded into a numeric field.

The packed decimal format uses half a byte to store each decimal digit. The sign is held in the final half byte: hex digit A, C, E, or F signifies a positive number; B or D a negative number. (The iSeries 400 natively uses only F or D to indicate sign, but it can interpret the other values.) If there is an even number of digits in the packed field, the first half byte will be unused, and will normally contain binary zeroes. The maximum length of an iSeries packed field is currently 30 digits, held in 16 bytes.

The first validation method, below, checks each byte of the test field. The final byte must take one of 60 possible valid values: x0A .. x0F, x1A ... x1F, ... x9A ... x9F. Each of the other bytes can take 100 possible values: x00 ... x09, x10 ... x19, ... x90 ... x99. The program overlays each byte with a one byte unsigned integer field, which is used as an index into one of two lookup tables. Note that the lookup tables are loaded first time in, and are declared as static so that they retain their value between calls to the subprocedure.

The second validation method uses SQL to...


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
iSeries programmer tips
Enhancing RPG with external SQL stored procedures
Tracking data changes on IBM i with triggers
Introduction to SQLRPGLE on IBM i: Making a report
Implementing a browser interface in COBOL: Displaying database fields
Taking advantage of CL advancements, starting with V5R3
TAATOOL: Useful tools for programmers on IBM i
Implementing a browser interface in COBOL: Creating your graphic Web page
Implementing a browser interface in COBOL: Getting started
Making the most of RPG data handling on IBM i
Groovy programming on IBM i

Application Development
iSeries calling an .exe
Top 10 programmer tips
Formatted work job scheduler
Convert system date and time
Mixing free format code with embedded SQL
SQL update a field in one file from a field in another file
Webcasts for iSeries programmers
Programming advice from the pros
Easy code copying via the drag and drop method
Setting FTP time-outs

iSeries CL programming
Taking advantage of CL advancements, starting with V5R3
Checking in on your IBM i authorization lists
Running PHP open source applications: NOBODY needs authority
Simplify the process of converting a spool file from iSeries into an Excel spreadsheet
CL program for daily backups
An automated CL method of moving a query from AS/400 to Excel
Changing user password expiration
Eight steps for creating program documentation using AS/400 utilities
DAYSPAST CLLE program for AS/400: Compares object creation date with today's date
Advanced Job Scheduler help

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


identify records that contain invalid decimal data. The first step is to use SQL's HEX function to return a hexadecimal representation of a field or substring. Next we use the TRANSLATE function to translate all valid sign digits ('A'-'F') to 's'. Any valid packed decimal string should now have the form 'nn...s', where n is a numeric hex digit and s is a literal 's'. Finally, we use the LOCATE function to ensure that there is precisely one sign digit, and that it is at the end of the hex string.

The final SQL statement, checking for one field, takes the form shown below. (A substring from a flat file can be substituted for fld, if required.) This method is useful if you have only a small number of suspect packed fields, and it has the advantage of allowing interactive display of the invalid records.

Finally, we could combine these two approaches by writing an SQL UDF (User Defined Function) that invokes subprocedure IsValidPacked. This would make it less cumbersome to validate multiple fields in the same SQL statement.



Code

Validation Method 1: Lookup Table

Validation Method 2: SQL

select * from file
where not
locate('s', translate(hex(fld),'ssssss','ABCDEF')) = 2*length(fld)


Rate this Tip
To rate tips, you must be a member of Search400.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



iSeries Security - Security Tools, Physical Security and System Security
HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts