Get length of value in a field


We can find the length of a field value filled in a Variable using %CHECKR (Check Reverse) and %len(%trimr(var)).

%CHECKR returns the last position of the string base that contains a character that does not appear in string comparator.

If all of the characters in base also appear in comparator, the function returns 0.


// Validate Subscriber Length         
  $Length = %CHECKR(' ':MBR_ID);  

// Validate Claim Length      
$Length = %CHECKR(' ':Claim);    


Use %len(%trim(var)) to get the length. Or use %trimr, %len(%trimr(var)), if the value might have leading blanks that should be counted as part of the length. For example, if the value is ' abc ' you might want the length 4, not 3.

%TRIMR function is used to trim trailing spaces of a string.

DRunCmd           PR                  EXTPGM('QCMDEXC')        
DCmdStr                        500A   CONST OPTIONS(*VARSIZE)  
DCmdLen                         15P 5 CONST   

Command_String = 'CLRPFM FILE(*LIBL/FILE)';         
RunCmd(Command_String: %Len(%TrimR(Command_String)));                 
If        Field <>*Blanks                   
          and %Len(%TrimR(FilFld)) > Field;
   Eval      Err_Flag='Y';
lv_Cmd   = 'ADDLIBLE LIB(' + lv_Lib + ') POSITION(*FIRST)';   
Cmdlen =  %len(%trimr(lv_Cmd));                               
Qcmdexec (lv_Cmd:Cmdlen);                                     


Go4AS400: %TRIMR


We can't use %Len or %Size to get the length of the value filled in a variable.

%LEN can be used to get the length of a variable.

  • For alphanumeric data, %LEN always returns the number of bytes.
  • For UCS-2 fields and graphic data, %LEN always returns the number of double bytes.

%SIZE function returns the number of bytes occupied by the element.

Post Comments