SEARCH ( SAP ABAP Keyword)

SEARCH is a keyword used in SAP ABAP programming.This tutorial covers its introduction & syntax details.

SEARCH

Variants

1. SEARCH f FOR g.
2. SEARCH itab FOR g.

Variant 1
SEARCH f FOR g.

Additions

1. … ABBREVIATED
2. … STARTING AT n1
3. … ENDING AT n2
4. … AND MARK

Effect
Searches the field f for the string in the field g . This string can have any of the following formats:
‘str’
a character string (trailing blanks are ignored) ‘.str.’ any character
string between the periods ‘*str’ a word ending with “str”, including
the word “str” ‘str*’ a word beginning with “str”, including the word
“str”
You can use the following characters as delimiters:

‘ ‘, ‘,’, ‘;’, ‘:’, ‘.’, ‘!’, ‘?’, ‘(‘, ‘)’, ‘+’, ‘/’ and ‘=’.

The return code value 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 .

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 a 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 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
(separated by other characters) 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, a field can be
anything 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’

Variant 2
SEARCH itab FOR g.

Additions

1. … ABBREVIATED
2. … STARTING AT lin1
3. … ENDING AT lin2
4. … AND MARK

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 in the field or table is found. 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. 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
the word must match.

Addition 2
… STARTING AT lin1

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

Addition 3
… ENDING AT lin2

Effect
Searches the internal table itab up to the line lin2 .

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:

DATA: BEGIN OF T OCCURS 100,
LINE(80),
END OF T.
MOVE ‘Alaska Texas ‘ TO T.
APPEND T.
MOVE ‘California Arizona ‘ TO T.
APPEND 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’
Related REPLACE , OVERLAY , SHIFT , SPLIT , TRANSLATE

Note
Performance
Searching
for the string ‘*str’ in an internal table is much more
runtime-intensive (approx. 500000 msn (standardized microseconds)) than
searching for ‘str*’ (approx. 10000 msn) or ‘str’ (approx. 35 msn). The
latter involves searching a table with 230 entries and 15 fields.
If
you perform a search in a field which is 35 bytes long for ‘*str’ or
‘str*’, the runtime consumption is approx. 600 msn, whereas searching
for ‘str’ takes about 25 msn.