IBM i 7.2 brought us two new CL built in functions (BIFs), %UPPER and %LOWER, that are used translate the contents of a variable or string to upper or lower case. I have always used the RPG BIF %XLATE to achieve the same result, I just wish IBM had added the same BIFs to RPG.
The syntax for both of these CL BIFs is similar to each other with just two parameters:
- Variable or string to convert - mandatory
- CCSID - optional
For example:
%UPPER(&VAR1) %UPPER('string') %UPPER(&VAR 37) %LOWER(&VAR1) %LOWER('string') %LOWER(&VAR 1208) |
CCSID 37 is for US English, 1208 is for Unicode UTF-8.
As I mentioned above I have had used RPG's %XLATE to translate upper and lower cases:
dcl-c UpperCase 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ; dcl-c LowerCase 'abcdefghijklmnopqrstuvwxyz' ; dcl-s String char(25) ; String = 'rpgpgm.com' ; String = %xlate(LowerCase:UpperCase:String) ; |
In CL I can just do the following:
CHGVAR VAR(&STRING_1) VALUE('rpgpgm.com') CHGVAR VAR(&STRING_2) VALUE(%UPPER(&STRING_1)) CHGVAR VAR(&STRING_3) VALUE(%LOWER(&STRING_2)) |
In debug I can see that the %UPPER had translated the characters to upper case, see &STRING_2. And then I can see that %LOWER has translated the characters to lower case, &STRING_3.
> EVAL &string_1 &STRING_1 = 'rpgpgm.com ' > EVAL &string_2 &STRING_2 = 'RPGPGM.COM ' > EVAL &string_3 &STRING_3 = 'rpgpgm.com ' |
The problem with using RPG's %XLATE is that I have to give all the characters I want to translate to and from. If I had to deal with multi lingual data the constant would get large to include all the letters with accents, circumflexes, umlauts, etc. The %UPPER and %LOWER translate these too with no extra coding:
CHGVAR VAR(&STRING_1) VALUE('áâãåæçèéêëìíîïñòóôõøùúûý') CHGVAR VAR(&STRING_2) VALUE(%UPPER(&STRING_1)) CHGVAR VAR(&STRING_3) VALUE(%LOWER(&STRING_2)) |
When I viewed the variables in debug I could see that all the characters were successfully translated:
> EVAL &string_1 &STRING_1 = 'áâãåæçèéêëìíîïñòóôõøùúûý ' > EVAL &string_2 &STRING_2 = 'ÁÂÃÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕØÙÚÛÝ ' > EVAL &string_3 &STRING_3 = 'áâãåæçèéêëìíîïñòóôõøùúûý ' |
Come on IBM you need to add these two BIFs to RPG in a future Technical Refresh!
You can learn more about these on the IBM website:
This article was written for IBM i 7.2.
Thank you to everyone who has pointed out that you can use SQL to convert from upper to lower case and vice versa. The details of how to do that will be in next week's post.
ReplyDeleteAnd also lowercase 'à ä ö ü þ' => to uppercase 'À Ä Ö Ü Þ'
ReplyDeleteChris Ringer
Thanks, Simon. A %PROPER BIF would be handy, too.
ReplyDelete(The "Preview"-Button seems to just reload the page, emptying the comment.)
ReplyDeleteIf you don't want to wait until IBM might come up with BIFs one day, API "QlgConvertCase" can be wrapped into functions which would do the same as their CL counterparts.
Learned New Thanks :)
ReplyDeleteWhy have 2 Programming languages. Add all CL functionality to RPG !
ReplyDeleteThe major CL function is the ability to execute commands. That need to be added to the RPG language. If that is done no need anymore to improve the CL LAnguage.
Some people would say that using QCMDEXC, QCAPCMD, or SYSTEM() allows you to run CL commands within another programming language.
ReplyDelete"Some people would say", but You know and we all know it is not the same.
Delete1. Need to build a string. It is not a problem, but it makes the code not so clean. Virtually almost all new proramming features could be done by manipulating strings, the question is how clean and cute it is.
2.Commands that return variables are imposible to do via qcmdexc.
If Qcmdexc would have been as simple as using CL commands, nobody would have used CL and this article would have been absolete.