HOME

FORUM

UPLOAD SOURCE

RPGLE/RPG

CLLE

SQLRPGLE

DDS

API

OTHER

JAVA

IFS

HTML

JAVA SCRIPT

PHP

MYSQL

XML

OLE DB





    Built in functions are actually procedures written by IBM that may be used in expressions (IF, EVAL, etc.) as if they are variables.   Procedures are an ILE concept that we will cover in later sections.    For now just think of a procedure as a kind of hybrid subroutine.

    All built in functions begin with a percent sign (%).   Most built in functions accept parameters which can be coded in parenthesis.   Multiple parameters should be separated by a colon (:).
    Link to what IBM says





    %CHAR

    	%CHAR(expression)
    	
    
  • Convert to Character Data

  • %CHAR converts the value of the expression from graphic, UCS-2, numeric, date, time or timestamp data to type character. The converted value remains unchanged, but is returned in a format that is compatible with character data.

  • DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++ D scramblename S 20G VARYING INZ(G'oXXYYZZi') D date S D INZ(D'2003/09/07') D time S T INZ(T'12:23:34') D result S 100A VARYING CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq.. CL0N01Factor1+++++++Opcode(E)+Extended-factor2+++++++++++++++++++++++++++ C EVAL result = 'It is ' + %CHAR(time) C + ' on ' + %CHAR(date) * result = 'It is 12:23:34 on 2003/09/07' * C EVAL result = 'The time is now ' C + %SUBST(%CHAR(time):1:5) + '.' * result = 'The time is now 12:23.' * C EVAL result = 'The customer''s name is ' C + %CHAR(scrambledName) + '.' * result = 'The customer's name is oXXYYZZi.'



    %CHECK

    	%CHECK( Compare : String { : start-position } )
    	
    
  • Works the same as the Op-code CHECK
  • It returns the position of the left-most character in String
    that is not one of the characters in Compare.

  • Search starts at specified start-position if supplied
  • Otherwise starts at beginning (%CHECK) or end (%CHECKR) of field
  • If all characters match, '0' is returned

  • C Digits Check InputString ErrorPos C If ErrorPos > 0 C Do (Something) C Enddo C* C* Replace with this C* C If %Check(ValidNumeric : InputString) > 0 C Do (Something) C Enddo


    %DATE

    	%Date( { expression { : date format } } )
    	
    
  • Converts a character or numeric field to a date
  • It performs the same function as a MOVE to a date

  • Second parameter specifies the format of the source field
  • Also works with Timestamps
  • If first parameter is omitted then the system date is returned

  • Duration types are the same as the ADDDUR/SUBDUR opcodes D CharDate1 S 6A Inz('011549') D CharDate2 S 6A Inz('031954') D ISODate S D DatFmt(*ISO) D USADate S D DatFmt(*USA) * Loading a date field from a character field prior to V5R1 C *MDY0 MOVE CharDate ISODate C *MDY0 MOVE CharDate USADate * The equivalent code in V5R1 C EVAL ISODate = %Date(CharDate1: *MDY0) C EVAL USADate = %Date(CharDate2: *MDY0)



    %DEC

    	%DEC(numeric expression{:precision:decimal places})
    	
    
  • Convert to Packed Decimal Format

  • %DEC converts the value of the numeric expression to decimal (packed) format with precision digits and decimal places decimal positions. The precision and decimal places must be numeric literals, named constants that represent numeric literals, or built-in functions with a numeric value known at compile-time.

  • DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++ D p7 s 7p 3 inz (1234.567) D s9 s 9s 5 inz (73.73442) D f8 s 8f inz (123.456789) D result1 s 15p 5 D result2 s 15p 5 D result3 s 15p 5 CL0N01Factor1+++++++Opcode&ExtExtended-factor2+++++++++++++++++++++++++++ C eval result1 = %dec (p7) + 0.011 C eval result2 = %dec (s9 : 5: 0) C eval result3 = %dech (f8: 5: 2) * The value of "result1" is now 1234.57800. * The value of "result2" is now 73.00000 * The value of "result3" is now 123.46000.

    %DIFF

    	%DIFF( ISODate1 : ISODate2 : Difference )
    	
    
  • Calculates a difference between date, time or timestamp values
  • It replaces the SUBDUR opcode

  • Other duration calculations are performed by simple + and - operators
  • In conjunction with new duration Built-Ins

    Duration types are the same as the ADDDUR/SUBDUR opcodes
    *MS - *MSECONDS, *S - *SECONDS, *MN -*MINUTES, *H - *HOURS,
    *D - *DAYS, *M - MONTHS, *Y - YEARS

    %EDITC

    %EDITC(numeric : editcode {: *ASTFILL | *CURSYM | currency-symbol})
    	
    
  • This function returns a character result representing the numeric value edited according to the edit code. In general, the rules for the numeric value and edit code are identical to those for editing numeric values in output specifications. The third parameter is optional, and if specified, must be one of:
  • Edit Value Using an Editcode

  • *ASTFILL
  • Indicates that asterisk protection is to be used. This means that leading zeros are replaced with asterisks in the returned value. For example, %EDITC(-0012.5 : 'K' : *ASTFILL) returns '***12.5-'.


  • *CURSYM
  • Indicates that a floating currency symbol is to be used. The actual symbol will be the one specified on the control specification in the CURSYM keyword, or the default, '$'. When *CURSYM is specified, the currency symbol is placed in the the result just before the first significant digit. For example, %EDITC(0012.5 : 'K' : *CURSYM) returns ' $12.5 ' .

    DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++ D msg S 100A D salary S 9P 2 INZ(1000) * If the value of salary is 1000, then the value of salary * 12 * is 12000.00. The edited version of salary * 12 using the A edit * code with floating currency is ' $12,000.00 '. * The value of msg is 'The annual salary is $12,000.00' CL0N01Factor1+++++++Opcode&ExtExtended-factor2+++++++++++++++++++++ C EVAL msg = 'The annual salary is ' C + %trim(%editc(salary * 12 C :'A': *CURSYM)) * the value of msg is 'The annual salary is &12,000.00' C EVAL msg = 'The annual salary is ' C + %trim(%editc(salary * 12 C :'A': '&')) * the value of msg is 'Salary is $*****12,000.00' * the '$' comes from the text, not from the edit code. C EVAL msg = 'Salary is $' C + %trim(%editc(salary * 12 C :'B': *ASTFILL)) * the value of msg is 'The date is 1/14/1999' C EVAL msg = 'The date is ' C + %trim(%editc(*date : 'Y'))
    Example 2
    DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++ D neg S 5P 2 inz(-12.3) D pos S 5P 2 inz(54.32) D editparens PR 50A D val 30P 2 value D editedVal S 10A CL0N01Factor1+++++++Opcode&ExtExtended-factor2++++++ C EVAL editedVal = editparens(neg) * Now editedVal has the value '(12.30) ' C EVAL editedVal = editparens(pos) * Now editedVal has the value ' 54.32 ' *---------------------------------------------------- * Subprocedure EDITPARENS *--------------------------------------------------- P editparens B D editparens PI 50A D val 30P 2 value D lparen S 1A inz(' ') D rparen S 1A inz(' ') D res S 50A * Use parentheses if the value is negative C IF val < 0 C EVAL lparen = '(' C EVAL rparen = ')' C ENDIF * Return the edited value * the '1' edit code does not include a sign so we * don't have to calculate the absolute value. C RETURN lparen + C %editc(val : '1') + C rparen P editparens E




    %LOOKUP

    	%LOOKUPxx(SearchFor : Array { : StartIndex {: # Elems} } )
    	
    
  • Provides the same basic function as the LOOKUP Op-Code
  • Unlike LOOKUP, SearchFor and Array do not have
    to match in size and decimal positions
    Also provides optional number of elements to
    search in addition to starting index

  • %LOOKUP - Searches for an exact match
  • %LOOKUPLE - Searches for an exact match, or the closest item lower
    There are also %LOOKUPLT, %LOOKUPGE, and %LOOKUPGT
    Returns the Index number if SearchFor is found
    Otherwise it returns Zero
    The LOOKUP Op-code would have set the index (if supplied) to 1
    Also %LOOKUP will not change the value of the starting index

    %lookup(argument:Search-array::)

    %SUBdt

    	Eval Days = %SubDt(ISODate:*D)
    	
    
  • %SubDt provides a freeform equivalent to EXTRCT
  • It works with Dates, Times and Timestamps

  • The first parameter identifies the source field
  • The second identifies the portion to be extracted
    Values are the same as for EXTRCT i.e.
    *MS, *S, *MN, *H, *D, *M and *Y
    C Eval DayNumber = %SubDt(ProcessDate:*D) C Eval DayOfMonth = %SubDt(%Date():*D) C Eval ThisMonth = MonthName(%SubDt(%Date():*M))


Examples posted by visitors




     Posted by: Jamie - In date = *iso dec 8,0   

       *
     c                   z-add     InDate        RunDate
     c                   eval      keydate7 =
     c                             %uns(%char(%Date(RunDate:*Iso):*Cymd0))
     c                   eval      keydate8 =
     c                             %uns(%char(%Date(RunDate:*Iso):*ISO0)) 
  

     Posted by: Jimmy Octane - BIF %Replace   

 Built-in function %REPLACE. Built-in function %REPLACE is new with V4R2. 
You can use it to construct a string value by specifying a string to insert or 
replace within another string. In certain cases, %REPLACE can work like 
catenation. 
In others, it can work like a combination of catenation and substringing.


The first two %REPLACE parameters are strings, either both character or both 
graphic. 
The first (the replacement string) is the string to be "replaced" into the 
second 
(the source string).

The third and fourth %REPLACE parameters are optional and must be numeric 
values. 
The third parameter represents the start position in the source string where 
the 
replacement string is to be placed. It defaults to 1 if not specified. The 
fourth 
parameter represents the number of characters to be replaced within the source 
string. 
If not specified, it defaults to the length of the replacement string. So, the 
expression

%REPLACE('ABC':'12345')

results in the value "ABC45".

The following table shows some additional samples of how %REPLACE works:

Expression                   Result 
%REPLACE('ABC':'12345':2)	'1ABC5' 
%REPLACE('ABC':'12345':6)	'12345ABC' 
%REPLACE('ABC':'12345':1:0)	'ABC12345' 
%REPLACE('ABC':'12345':2:4)	'1ABC' 
%REPLACE('ABC':'12345':3:1)	'12ABC45'
  

     Posted by: Jimmy - %lookup examples   

      C                   Eval      Idx = %LookUp( SrchArg : SomeAry )
     C                   If        Idx > *Zero
      * Process information
     C                   EndIf
     C                   Eval      Idx = %LookUp( SrchArg : SomeAry : 4 )
     C                   If        Idx > *Zero
      * Process information
     C                   EndIf
     C                   Eval      Idx = %LookUp( SrchArg : SomeAry : 4 : 5 )
     C                   If        Idx > *Zero
      * Process information
     C                   EndIf
     C                   Eval      Idx = %LookUpGE( SrchArg : SomeAry )
     C                   If        Idx > *Zero
      * Process information
     C                   EndIf
     C                   Eval      *In01 = %TLookUpLT( SrchArg : TabEmp )
     C                   If        *In01
      * Process information
     C                   EndIf
     C                   If        %TLookUpLT( SrchArg : TabEmp )
      * Process information
     C                   EndIf 

  

     Posted by: Jimmy - %check examples   

      C                   Eval      Pos = %Check( ' ' : SomeString )
     C                   If        Pos > *Zero
      * Process information
     C                   EndIf
     C                   Eval      Pos = %Check( ' ' : SomeString : 11 )
     C                   If        Pos > *Zero
      * Process information
     C                   EndIf
     C                   Eval      Pos = %Check( '0123456789' : SomeString )
     C                   If        Pos > *Zero
      * Process information
     C                   EndIf
     C                   Eval      Pos = %CheckR( ' ' : SomeString )
     C                   If        Pos > *Zero
      * Process information
     C                   EndIf
     C                   Eval      Pos = %CheckR( ' ' : SomeString : 30 )
     C                   If        Pos > *Zero
      * Process information
     C                   EndIf 
  

     Posted by: Robert   

  %scan
D SrchStrng       S             17a   Inz(*blanks)
D GoodRun         S              1a 
D x7text          S             50a 
 
C                   Eval      SrchStrng='transfer complete'
C                   Eval      x7text = 'Content scanned; transfer complete.'

 * %scan will return the starting position of the SrchStrng. 
C                   If        %scan(%trim(srchstrng):x7text) > 0
C                   Eval      GoodRun='Y'
C                   Endif                  

     Posted by: Manow : %Len & %Trim   

 C                   MOVE      *BLANKS       T020             20                           
C                   MOVE      *BLANKS       TA10             10                           
C                   MOVE      *BLANKS       TB10             10                           
C                   MOVE      *BLANKS       TC10             10                           
C                   MOVE      *ZEROS        LEN               3 0                         
 *                                                                                        
C                   EVAL      T020 ='023185600-16'                                         
 * LEN = 12 - 10 = 2                                                                      
C                   EVAL      LEN = %LEN(%TRIM(T020)) - 10                                
C                   EVAL      TA10 = %TRIM(%SUBST(T020:11:LEN))                           
 * LEN =  2   / TA10 = '16        '                                                       
C                   EVAL      LEN = %LEN(%TRIM(TA10))                                     
 * TB10 = '023185600'                                                                     
C                   EVAL      TB10 = %SUBST(T020:1:9)                                     
 * TB10 = '0231856' + '16'                                                                
C                   EVAL      TC10 = %SUBST(T020:1:9-LEN) + %TRIM
(TA10)                   
C     TA10          DSPLY                                               
 * TA10 = '023185600-16'                                                
C     TB10          DSPLY                                               
 * TB10 = '023185600'                                                   
C     TC10          DSPLY                                               
 * TC10 = '023185616'                                                   
C                   MOVE      *ON           *INLR                        

     Posted by: Jeremy - Remove CR/LF/NL using %scan , %Xlate,   

 *   CR = Carriage Return    
*   LF = Line Feed          
*   NL = New Line    
Data  is a string of any data(field) that has CR/LF/NL embedded in it. 

D CR              C                   Const(X'0D')   
D LF              C                   Const(X'25')   
D NL              C                   Const(X'15')   
D pos             s              4  0     
D chg             s              1             
/free                                                    
                            pos = 0;                     
                            chg = ' ';                   
                read      rSomefile;                       
                   dow   not%eof;                        
                      pos = %scan (CR:data);             
                       if pos > 0;                       
                            data = %Xlate(CR:' ':data);  
                            chg = 'Y';                   
                       endif;             
 ************* do this for each CR/LF/NL to remove them. Update if Change  
<> " "      
  
Eval *inlr = *on
/end-free                

     Posted by: SUDHARSAN S   

 In a display file we a date input(*DMY) from input field INP.
then we convert it to *USA format using %date,and pass it to the output field 
OUT.

C                   DOW       NOT *IN03                              
C                   EXFMT     DATEF                                     
C   03              LEAVE                                               
C                   CLEAR                   ERRMSG                      
C     *DMY          TEST(D)                 INP                    10   
C   10              EVAL      ERRMSG='INVALID DATE'                     
C   10              ITER                                                
C                                                                       
C                   EVAL      out=%INT(%CHAR(%date(INP:*DMY):*USA0))    
C                   ENDDO                                               
C                   SETON                                            LR 
 

     Posted by: jmv   

  When you are signed on to an AS400, you can view your job log by keying the 
command DSPJOBLOG. Then you must hit F10 (to see details) and use PAGE UP to 
see the log. 
 

     Posted by: JMV   

  darr1                        5 0  dim(5)

c       eval arr1[1] =10
c     eval arr1[2]=10
c     eval arr1[3]=10
c      eval arr1[4]=10
c         eval arr1[5]=10

c           eval sum = %xfoot(arr1)
summing the array using %xfoot 

ans sum = 50
 


Post your BIF examples

(must enter name and example to post)



Your Name/Description:
BIF's:







About Code400.com | resume | Search | Site Map | Suggestions
© Copyright 2003-2008 Code400.com



Thursday Mar 11, 2010 @ 7:16 PM