Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

SEARCH Statement

Former Member
0 Kudos

hi frnd's,

DATA:

str(100) TYPE c VALUE 'Abap object '.

SEARCH str FOR 'Object'.

WRITE:/ sy-subrc, sy-fdpos, sy-tabix.

SEARCH str FOR '. .'.

WRITE:/ sy-subrc, sy-fdpos, sy-tabix.

SEARCH str FOR 'OBj*'.

WRITE:/ sy-subrc, sy-fdpos, sy-tabix.

Can anybody explain me this code .

thanks in advance

suganya.d

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Suganya,

Please read below.

SEARCH f FOR g.

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

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

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.

I will strongly recommend you to execute the code and observe the fields, you will be able to understand the same.

Message was edited by: Chaitanya Kovvuri

5 REPLIES 5

Former Member
0 Kudos

Hi Suganya,

Please read below.

SEARCH f FOR g.

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

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

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.

I will strongly recommend you to execute the code and observe the fields, you will be able to understand the same.

Message was edited by: Chaitanya Kovvuri

Former Member
0 Kudos

Hi,

Consider your code.

REPORT abc.

DATA:
str(100) TYPE c VALUE 'Abap object '.

SEARCH str FOR 'Object'.
WRITE:/ sy-subrc, sy-fdpos, sy-tabix.
*Result :    0      5           1

String 'Object' found so sy-subrc =0.

String 'Object' found in str at 5 positon (012345) so sy-fdpos = 5.

Its a single record(not an internal table so sy-yabix = 1)

SEARCH str FOR '. .'.
WRITE:/ sy-subrc, sy-fdpos, sy-tabix.
*Result :    0      4           1

space found so sy-subrc =0.

space found in str at 4 positon (012345) so sy-fdpos = 4.

Its a single record(not an internal table so sy-yabix = 1)

SEARCH str FOR 'OBj*'.
WRITE:/ sy-subrc, sy-fdpos, sy-tabix.
*Result :    0      5           1

String starting with 'Obj*' found so sy-subrc =0.

String starting with 'Obj*' found in str at 5 positon (012345) so sy-fdpos = 5.

Its a single record(not an internal table so sy-yabix = 1)

SY-SUBRC = 0:

The search string < > was found in the field < >.

SY-FDPOS contains the offset of the found string or the found word within the field.

SY-SUBRC = 4:

The search string < > was not found in the field < >.

SY-TABIX is in case of searching in Internal tables. It contains the index where the record was found in the Internal table.

<b>SEARCH str FOR 'OBj*'.</b>

>>>Search <b>str</b> for pattern starting with <b>OBj</b>

<b>SEARCH str FOR '. .'.</b>

>>>Search <b>str</b> for any full points with space <b>'. .'</b>

Regards,

Arun Sambargi.

Message was edited by: Arun Sambargi

Former Member
0 Kudos

Hi Suganya,

This code generally gives a good idea of how search statements works.

DATA:

str(100) TYPE c VALUE 'Abap object '.

      • searching for the string Object in the STR field

SEARCH str FOR 'Object'.

      • Displaying the sy-subrc value

***SY-FDPOS contains the offset of the found string

      • and similarly the tabix which is 1 for the case here

WRITE:/ sy-subrc, sy-fdpos, sy-tabix.

      • . . means finding the space in the field

SEARCH str FOR '. .'.

WRITE:/ sy-subrc, sy-fdpos, sy-tabix.

    • again * is a wild card key finder words starting with OBJ including obj

SEARCH str FOR 'OBj*'.

WRITE:/ sy-subrc, sy-fdpos, sy-tabix

Hope this helps.

Regards,

kinshuk

Former Member
0 Kudos

Hi......

To search a character field for a particular pattern, use the SEARCH statement as follows:

SEARCH <c> FOR <str> <options>.

The statement searches the field <c> for <str> starting at position <n1>. If successful, the return code value of SY-SUBRC is set to 0 and SY-FDPOS is set to the offset of the string in the field <c>. Otherwise, SY-SUBRC is set to 4.

Regards

Abhishek

kanthimathikris
Employee
Employee
0 Kudos

Hi,

SY-SUBRC defines whether the search is successful or not.

if sy-subrc = 0, the search is successful else it is

unsuccessful.

SY-FDPOS gives the position of the search string if found.

SY-TABIX - The search keyword can also be used in internal tables. In this case SY-TABIX gives the record number (line) in the internal table.

if the keyword is not used with tables, but used with simple datetypes as in ur case, the value of SY-TABIX is 1.

str(100) TYPE c VALUE 'Abap object '.

SEARCH str FOR 'Object'.

WRITE:/ sy-subrc, sy-fdpos, sy-tabix.

The output was 0,5,1

means SEARCH is not case sensitive

SEARCH str FOR '. .'.

WRITE:/ sy-subrc, sy-fdpos, sy-tabix.

The output was 0,4,1

means SEARCH searches for characters between '. .'.

Here it search for space.

SEARCH str FOR 'OBj*'.

The output was 0,5,1

means SEARCH searches for string 'OBj*'

Regards.