SEARCH

Variants:

1. SEARCH f    FOR g.

2. SEARCH itab FOR g.

Depending on whether byte or character string processing is carried out, the operands can only be byte-type or character-type (see Overview).

See Only character fields allowed in string processing.

In some cases, the syntax rules that apply to Unicode programs are different than those for non-Unicode programs. For more information, see Character String Processing.

Variant 1

SEARCH f FOR g.

Extras:

1. ... ABBREVIATED

2. ... STARTING AT n1

3. ... ENDING   AT n2

4. ... AND MARK

5. ... IN BYTE MODE

6. ... IN CHARACTER MODE

Effect

Searches the contents of f for the string in the field g. This string can have any of the following formats:

'str'
a character string (trailing spaces are ignored)
'.str.'
any character string between the periods
'*str'
a word ending with "str", including "str"
'str*'
a word beginning with "str", including "str"
"*str*'
this combination does not return the desired result but the word "str" ending with an asterisk (*). You should therefore avoid using this search variant.

You can use any non-alphanumeric character as a separator, that is, spaces, punctuation marks and non-printable characters.

The system does not distinguish between upper and lower case characters.

The Return Code is set as follows:

SY-SUBRC = 0:
The search string g was found in the field f. SY-FDPOS contains the offset of the found string or the found word within the field.
SY-SUBRC = 4:
The search string g was not found in the field f.

Notes

  1. The statement SEARCH has been replaced with the statement FIND in Release 6.10.

  2. The search patterns 'str' and '.str.' are identical apart from a few exceptions. You must use '.str.' when the pattern str contains spaces (at the end), the '.' character (at the beginning and end), or the '*' character (at the end). You should also use '.str.' when the search string str is variable and you cannot predict when you write the statement what the contents of the string will be.


Addition 1

... ABBREVIATED

Effect

Searches the field f for a word containing the character string specified in the field. Here, the characters specified in g may be separated by other characters in the word. If the string g occurs in a word, the return code in system field SY-SUBRC is set to 0. The first letter of the search string g and of the word must match.

Example

DATA F(50).
MOVE 'Alaska Texas California' TO F.
SEARCH F FOR 'Clfrn' ABBREVIATED.

Here, SY-SUBRC is set to 0 since not only does the string 'Clfrn' occur in 'California', but 'Clfrn' and 'California' begin with the same letter.

Addition 2

... STARTING AT n1

Effect

Searches the field f starting from the position n1. Here, n1 can also be a field containing the corresponding value. The first character in the field f is in position 1.
When you use the addition STARTING AT, the position specified for the found pattern in SY-FDPOS does not refer to the start of the field, but to the position n1.

Addition 3

... ENDING AT n2

Effect

Searches the field f up to the position n2.

Addition 4

... AND MARK

Effect

If the search string g is found, all the characters of the search string and all the characters occurring in between (in the case of SEARCH ABBREVIATED) are converted to upper case in the field f.

Example

DATA F(20) VALUE 'Peter Paul Mary'.
SEARCH F FOR '*UL' AND MARK.

SY-SUBRC is now set to 0, since the search string was found in 'Paul'. SY-FDPOS has the value 6, since the character string found starts at the offset 6. Also, the search string is marked, so that the new contents of F are as follows:
'Peter PAUL Mary'

Addition 5

... IN BYTE MODE

Effect

This addition can be used in combination with the additions STARTING AT and ENDING AT. All fields must be of the type X or XSTRING. Only 'str' is allowed as a search pattern.

Addition 6

... IN CHARACTER MODE

Effect

The addition is optional and corresponds to the default setting (see above).

Variant 2

SEARCH itab FOR g.

Extras:

1. ... ABBREVIATED

2. ... STARTING AT line1

3. ... ENDING   AT line2

4. ... AND MARK

5. ... IN BYTE MODE

6. ... IN CHARACTER MODE

Effect

Searches the internal table itab for the string in field g. The string can have the same format as in variant 1. The value of SY-SUBRC is set to 0, if the search string is found in the table. The system field SY-TABIX then contains the number of the table line where the string was found. Meanwhile, SY-FDPOS specifies the offset of the found string within the table line.

Note

The statement does not search the header line of an internal table itab.

Addition 1

... ABBREVIATED

Effect

As with SEARCH ABBREVIATED, searches the internal table itab for a word that contains the character string specified in the field g. Here, the characters specified in g can be separated by other characters in the word. The return code value of the system field SY-SUBRC is set to 0, if the string g occurs in a word. The first letter of the search string g and of the word must match.

Addition 2

... STARTING AT line1

Effect

Searches the internal table itab starting from line line1 to the end. line1 can be a field that contains the corresponding values.

Addition 3

... ENDING AT line2

Effect

Searches the internal table itab up to the line line2.

Addition 4

... AND MARK

Effect

If the search string g is found, all the characters of that search string and all the characters occurring in between (in the case of SEARCH ABBREVIATED) are converted to upper case in the internal table itab.

Example

Let T be an internal table which is empty:

TYPES: BEGIN OF T_TYPE,
        LINE(80),
      END OF T_TYPE.

DATA: T TYPE STANDARD TABLE OF T_TYPE WITH
             NON-UNIQUE DEFAULT KEY,
      WA_T TYPE T_TYPE.

MOVE 'Alaska Texas       ' TO WA_T.
APPEND WA_T TO T.
MOVE 'California Arizona ' TO WA_T.
APPEND WA_T TO T.
SEARCH T FOR '*ONA' AND MARK.

SY-SUBRC is now set to 0 because the search string was found in 'Arizona'. SY-TABIX contains the value 2 because 'Arizona' appears in the second line of the table T. SY-FDPOS is set to 11 because the found character string begins at the offset 11. Also, the search string was marked in the second line in such a way that the contents of that line now look as follows:
'California ARIZONA'

Addition 5

... IN BYTE MODE

Effect

This addition can be used in combination with the additions STARTING AT and ENDING AT. All fields must be of the type X or XSTRING. Only 'str' is allowed as a search pattern.

Addition 6

... IN CHARACTER MODE

Effect

The addition is optional and corresponds to the default setting (see above).

Note

Performance:

Searching generically for a string in an internal table is much more runtime-intensive (approx. 5000 msn (standardized microseconds)) than searching for 'str' (approx. 20 msn). This was measured using a table with 200 entries and 10 fields.
If you perform a search in a field which is 50 bytes long for '*str' or 'str*', the runtime consumption is approx. 300 msn, whereas searching for 'str' takes about 15 msn.

Related

REPLACE, OVERLAY, SHIFT, SPLIT, TRANSLATE, FIND

Additional help

Searching in Strings

Searching Table Lines for Strings