Change Case in RPG
Changing SENTENCE CASE didnt used to be something that the grey haired AS400 and iSERIES programmers ever worried about. Back in the pre-internet days, most data entry was in UPPERCASE, plugged into giant green on black terminals by people wearing 1970’s flares and thick glass spectacles. But now it’s a new age of Internet connectivity, webservices and UPPER to lower using XLATE %BIF’s in modern RPG.
Why do we care about sentence CASE checking so much?
Simply put – in programmig terms the word “Muffin” is not the same as the work “muffin” which is also different to “MuFfiN”. You see?
So, changing an input string (for example parameter sent in from a webservices request) to the same CASE as the database makes comparing you range of exciting tasty “muffins” much easier.
Changing sentence case is easy with RPG
dcl-c lowerCase ('abcdefghijklmnopqrstuvwxyz');
dcl-c upperCase ('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
dcl-s inputString varchar(200);
dcl-s outputString varchar(200);
//CONVERT FROM UPPERCASE TO LOWERCASE
inputString = 'Convert from lowerCase to upperCase';
outputString = %xlate(lowercase:uppercase:inputString);
dsply outputString;
//CONVERT FROM LOWERCASE TO UPPERCASE
inputString = 'CONVERT FROM UPPERCASE TO LOWERCASE';
outputString = %xlate(uppercase:lowercase:inputString);
dsply outputString;
Changing sentence case is easier with SQL RPG
But it’s even easier with RPG and SQL because you dont even need the XLATE variables and this will let you catch all the special characters for that language:
dcl-s inputString varchar(200);
dcl-s outputString varchar(200);
CharField = 'Hello World';
//CONVERT FROM UPPERCASE TO LOWERCASE
inputString = 'Convert from lowerCase to upperCase';
EXEC SQL set :outputString = upper(:inputString);
I will leave the syntax for the lower to your imagination 😉
PS: it looks like this:
//CONVERT FROM LOWERCASE TO UPPERCASE
inputString = 'CONVERT FROM UPPERCASE TO LOWERCASE';
EXEC SQL set :outputString = lower(:inputString);
In the %xlate function code, your comments seem to be reversed.
The xlate is correct it goes in the format xlate(from:to:value) Make sense?
You can check the IBM docs at https://www.ibm.com/docs/en/i/7.1?topic=functions-xlate-translate