How to test for RPG numeric values in alphameric fields? 

 September 9, 2013

By  NickLitten

IBM RPG is a lovely language.

RPGLE (as the latest version is known) has got some heritage. Some might say its an old language, but I say it’s kept young by its constantly evolving syntax! Testing numeric values has evolved over years, lets look at two examples.

The current version of RPGLE (IBM i V7R1 at time of writing this) has free form code, multitudes of built in functions and handles big system database crunching with as much ease as it handles little web page servlets. RPG’s evolved over 20+ years from an archaic and mystical thing bound by the rules of something called ‘the logic cycle’ into the modern power language for all IBM i Systems everywhere.

Personally, I find a few of the older functions still a bit annoying and wish there was a nicer way of handling them.

In this case, it’s time to convict RPGLE’s ability of checking data variables to see if they contain numeric or alphanumeric variables:

Your Honor – I present the defendant: TESTN

TEST(N) Is just a little bit smelly when it comes to really/truly finding out if a field is numeric or not: Try to add two values together and you must remember that ‘ 1’ is not the same as ‘___1’ or ‘0001’. So, we need to CHECK that a value is truly numeric when importing from a alpha field into a numeric field. Otherwise its… put the value in a numeric field and try to do something ‘numericish’ with it and *boom*

The other problem is that at time of writing – their is no RPG /FREE version of the TESTN built in function. For shame šŸ™

Luckily we have many many ways of checking for numerics.

My two top favorites are:

The Elegant Method testing RPG numeric values in alphameric fields

You can use this to test numeric w/o indicators.

RPG numeric values in alphameric
dcl-s Numbers char(10) Inz('0123456789') If %check(Numbers:My_Field) > 0; Error = *On; // it has non-numerics in it else; Error = *off; // everything is numeric #huzzah! EndIf;

This speaks for itself if you understand RPG.

If you dont understand RPG, then what are you doing wasting time reading this technical blurb? huh? Get on Facebook and starting clicking LIKE on random images.

Another way – The Lazy Man Method

OK – so lets admit that this is possibly the laziest technique ever.

EVAL WorkField = %dec(Anyfield:9: )
...... do whatever

Basically we are not bothering to check the field to see if its numeric but simply wrapping an error handler around it, so that if it bombs we ignore the *bomb* and do something else to fix the data. Lazy – Yes. Stupid – No!


IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Get In Touch

Iā€™m always looking for awesome input, feedback and critique!


Snug CBD

 20% Discount

I have partnered with SNUG CBD givING you Organic CBD
20% discount code "NL20"