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: 

String search syntax

Former Member
0 Kudos

Hi All,

I would like to know what is the syntax to be used to check if a particular word exists in a sentence. I tried the below, but it is not giving me the desired result. I am checking to see if the word Comp exists in the name & also would like to know is this a case-sensitive search? i.e would Comp be treated differently from comp? if i want the search to check for 'comp' irrespective of the case, how should i do it?

SELECT SINGLE NAME1 FROM KNA1 INTO Y_NAME1

WHERE KUNNR EQ VBRK-KUNRG.

IF Y_NAME1 CA 'Comp'.

Y_TYP_SAL = 'INTERNAL'.

ELSE.

Y_TYP_SAL = 'EXTERNAL'.

ENDIF.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

IF Y_NAME1 CS 'Comp'.

Y_TYP_SAL = 'INTERNAL'.

ELSE.

Y_TYP_SAL = 'EXTERNAL'.

ENDIF.

Just use this CS in ur comaprision operator .

Vijay

6 REPLIES 6

Former Member
0 Kudos

Hi,

use the find keyword to search the string

FIND 'Comp' IN Y_NAME1.

if sy-subrc = 0.

sucess

else

fail.

endif.

regards,

siva chalasani.

Reward points if found usefull

Former Member
0 Kudos

DATA:

c TYPE STRING,

p(2) TYPE C.

c = 'Everyone knows this'.

p = 'NO'.

FIND p IN c IGNORING CASE.

Try this.

Awrd Points if useful

Bhupal

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.

<str>----


Function

<pattern>--


Searches for <pattern> (any sequence of characters).--


Trailing blanks are ignored.

.<pattern>.----


Searches for <pattern>. Trailing blanks are not ignored.

*<pattern>----


A word ending with <pattern> is sought.

<pattern>*----


Searches for a word starting with <pattern>.

Words are separated by blanks, commas, periods, semicolons, colons, question marks, exclamation marks, parentheses, slashes, plus signs, and equal signs.

<option> in the SEARCH FOR statement can be any of the following:

ABBREVIATED

Searches the field <c> for a word containing the string in <str>. The characters can be separated by other characters. The first letter of the word and the string <str> must be

the same.

STARTING AT <n1>

Searches the field <c> for <str> starting at position <n1>. The result SY-FDPOS refers to the offset relative to <n1> and not to the start of the field.

ENDING AT <n2>

Searches the field <c> for <str> up to position <n2>.

AND MARK

If the search string is found, all the characters in the search string (and all the characters in between when using ABBREVIATED) are converted to upper case.

Ex.

DATA STRING(30) VALUE 'This is a little sentence.'.

WRITE: / 'Searched', 'SY-SUBRC', 'SY-FDPOS'.

ULINE /1(26).

SEARCH STRING FOR 'X'.

WRITE: / 'X', SY-SUBRC UNDER 'SY-SUBRC',

SY-FDPOS UNDER 'SY-FDPOS'

SEARCH STRING FOR 'itt '.

WRITE: / 'itt ', SY-SUBRC UNDER 'SY-SUBRC',

SY-FDPOS UNDER 'SY-FDPOS'

SEARCH STRING FOR '.e .'.

WRITE: / '.e .', SY-SUBRC UNDER 'SY-SUBRC',

SY-FDPOS UNDER 'SY-FDPOS'.

SEARCH STRING FOR '*e'.

WRITE: / '*e ', SY-SUBRC UNDER 'SY-SUBRC',

SY-FDPOS UNDER 'SY-FDPOS'.

SEARCH STRING FOR 's*'.

WRITE: / 's* ', SY-SUBRC UNDER 'SY-SUBRC',

SY-FDPOS UNDER 'SY-FDPOS'.

Regards,

Bhaskar

Former Member
0 Kudos

Try this logic

parameters: p_matnr like mara-matnr.

data: it_mara like mara occurs 0 with header line.

replace all occurrences of '*' in p_matnr with '%' .

SELECT * FROM mara INTO table it_mara WHERE matnr LIKE p_matnr.

loop at it_mara.

write:/ it_mara-matnr.

endloop.

if usefull reward points helpfull

Former Member
0 Kudos

IF Y_NAME1 CS 'Comp'.

Y_TYP_SAL = 'INTERNAL'.

ELSE.

Y_TYP_SAL = 'EXTERNAL'.

ENDIF.

Just use this CS in ur comaprision operator .

Vijay

0 Kudos

Hi Sniper,

Thank you very much for your help.

Note for Bhaskar: Thanks a lot for that explanatory post

Note for others: Thank you for providing different alternatives, but when i tried with FIND, system prompted a syntax error, it said FIND is undefined.

Keep the good work going.

Cheers & Regards,

Vivek