POSITION: Returning the Position of a Search String in a Source String

How to:

Given a search string, a source string, and a starting position, POSITION returns the position of the search string within the source string. The search starts at the given starting position and searches from left to right. If the string is not found, POSITION returns zero (0). The search is case sensitive.

Syntax: How to Return the Position of a Search String in a Source String

POSITION(search, source, start)

where:

search

Alphanumeric

Is the search string.

source

Alphanumeric

Is the source string.

start

Numeric

Is the starting position in the source string for the search.

Example: Returning the Position of a Search String in a Source String

The following request uses POSITION twice, once to find the first occurrence of the character A after starting position 3 in FULLNAME, and once to find the first occurrence of the character a after starting position 3 in FULLNAME.

TABLE FILE WF_RETAIL_CUSTOMER
PRINT FULLNAME 
COMPUTE POS1/I5 = POSITION('A', FULLNAME, 3) ;
POS2/I5 = POSITION('a', FULLNAME, 3) ;
WHERE RECORDLIMIT EQ 5
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.