Wednesday, February 18, 2015

CL Built in Functions to Change Case

built in function to change upper to lower case %bif

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:

  1. Variable or string to convert - mandatory
  2. 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.

8 comments:

  1. 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.

    ReplyDelete
  2. And also lowercase 'à ä ö ü þ' => to uppercase 'À Ä Ö Ü Þ'

    Chris Ringer

    ReplyDelete
  3. Thanks, Simon. A %PROPER BIF would be handy, too.

    ReplyDelete
  4. (The "Preview"-Button seems to just reload the page, emptying the comment.)
    If 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.

    ReplyDelete
  5. Rajeshbabu JayaramanMarch 3, 2015 at 7:04 AM

    Learned New Thanks :)

    ReplyDelete
  6. Why have 2 Programming languages. Add all CL functionality to RPG !

    The 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.

    ReplyDelete
  7. Some people would say that using QCMDEXC, QCAPCMD, or SYSTEM() allows you to run CL commands within another programming language.

    ReplyDelete
    Replies
    1. "Some people would say", but You know and we all know it is not the same.

      1. 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.

      Delete

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.