SEARCHR

SEARCHR searches for the first occurrence of any one of the elements of a string within another string but the search starts from the right.

Read syntax diagramSkip visual syntax diagramSEARCHR( x, y, n)
The SEARCHR function performs the same operation as the SEARCH built-in function except for the following differences:
  • The search is done from right to left.
  • The default value for n is LENGTH(x).
  • Unless 0 ≤ n ≤ LENGTH(x), the STRINGRANGE condition, if enabled, is raised. Its implicit action and normal return give a result of zero.

The BIFPREC compiler option determines the precision of the result returned.

SEARCHR will perform best when the second and third arguments are either literals, named constants declared with the VALUE attribute, or restricted expressions.

Example

  dcl Source char value (' 555 Bailey Ave, San Jose, CA 95141, USA');
  dcl Digits char value ('0123456789');
  dcl (Start, End) fixed bin(31);
  dcl Num char(20) var;

     /*   Find last number (i.e., zip code)    */

  End   = searchr (Source, Digits);        /* returns 35 for the '1' */
  Start = verifyr (Source, Digits, End);   /* returns 30 for the ' ' */
  Num   = substr (Source, Start + 1, End - Start);  /* extract number */