FIELD function

Syntax

FIELD (string, delimiter, occurrence [ ,num.substr] )

Description

Use the FIELD function to return one or more substrings located between specified delimiters in string.

delimiter evaluates to any character, including field mark, value mark, and subvalue marks. It delimits the start and end of the substring. If delimiter evaluates to more than one character, only the first character is used. Delimiters are not returned with the substring.

occurrence specifies which occurrence of the delimiter is to be used as a terminator. If occurrence is less than 1, 1 is assumed.

num.substr specifies the number of delimited substrings to return. If the value of num.substr is an empty string or less than 1, 1 is assumed. When more than one substring is returned, delimiters are returned along with the successive substrings.

If either delimiter or occurrence is not in the string, an empty string is returned, unless occurrence specifies 1. If occurrence is 1 and delimiter is not found, the entire string is returned. If delimiter is an empty string, the entire string is returned.

If string evaluates to the null value, null is returned. If string contains CHAR(128) (that is, @NULL.STR), it is treated like any other character in a string. If delimiter, occurrence, or num.substr evaluate to the null value, the FIELD function fails and the program terminates with a run-time error message.

The FIELD function works identically to the GROUP function.

Examples

D=FIELD("###DHHH#KK","#",4)
PRINT "D= ",D

The variable D is set to DHHH because the data between the third and fourth occurrence of the delimiter # is DHHH.

REC="ACADABA"
E=FIELD(REC,"A",2)
PRINT "E= ",E

The variable E is set to "C".

VAR="?"
Z=FIELD("A.1234$$$$&&",VAR,3)
PRINT "Z= ",Z

Z is set to an empty string since "?" does not appear in the string.

Q=FIELD("+1+2+3ABAC","+",2,2)
PRINT "Q= ",Q

Q is set to "1+2" since two successive fields were specified to be returned after the second occurrence of "+".

This is the program output:

D=       DHHH
E=       C
Z=
Q=       1+2