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: 

select the value ending with 'S'

Former Member
0 Kudos

Hello ABAP experts,

I am a BW guy, not much into ABAP. There is a small coding requirment. I am not sure if this can be done in one statment.

select the value from the table which has value ending with 'S'.

Solutions

-- I can loop the table with 'LOOP'

-- is there any statment to achive the value ending with 'S'

Any suggestions are highly valuable.

Many thx in advance.

Sandhya

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Sandhya,

Sorry, but i dont understand the question.

You have an internal table, and you need to look for a value that ends with S. The internal table has an unique field ? a lot of fields ? you haev to look for the value always in the same field of the internal table ?

Waiting for your answer to help !

Regards

PabloX

17 REPLIES 17

Former Member
0 Kudos

Hi Sandhya,

Sorry, but i dont understand the question.

You have an internal table, and you need to look for a value that ends with S. The internal table has an unique field ? a lot of fields ? you haev to look for the value always in the same field of the internal table ?

Waiting for your answer to help !

Regards

PabloX

nablan_umar
Active Contributor
0 Kudos

You can do like this.

tables: table1.

select * from table1 where fieldname1 like '%S'.

write: / table1.

endselect.

Former Member
0 Kudos

Hello Sandhya-

I am assuming that you want to run a Select query on a table and find values that end in 'S' for a particular field in that table.

To do this you can use the % wildcard operator. % is used to denote a sequence of any characters (including spaces). So, you might get the results you are looking for if you used a select query that looked like:

SELECT FIELD_A FROM TABLE WHERE FIELD_B LIKE '%S'.

; This line of code should return from TABLE the list of items in the FIELD_A that have values in FIELD_B that end in 'S'.

Ofcourse you can use this information and prepare more elaborate WHERE clauses that check for contents of multiple fields and find ones that, for example, end in 'S'.

If you feel that this helps then please assign some points.

Thank you,

- Vik.

0 Kudos

Thank you all.

This is the place I am trying to use the logic.

loop at ITAB1.

s_len = strlen( ITAB1-PTR1 ) - 1.

clear p_center.

p_center = ITAB1-PTR1+s_len(1).

If p_center ca 's'

or p_center eq 's'.

delete ITAB1 index sy-tabix.

continue.

else.

continue.

endif.

endloop.

So it can be modified like this. Wondering if this is correct and means the same thing.

loop at ITAB1.

if ITAB1-PTR1 ca '%S'

or ITAB1-PTR1 ca '%s'.

delete ITAB1 index sy-tabix.

continue.

else.

continue.

endif.

endloop.

Regards,

Sandhya

Message was edited by: Sandhya

0 Kudos

No it's not correct.

if ITAB1-PTR1 ca 's'. ( but you can't test the place )

0 Kudos

Hi Sandhya,

Looks like you want to find the records for which teh value of the field PTR1 ends in S(uppercase as opposed to lower case) and then delete it from your internal table.

Your initial logic is correct except that I am not sure if you want to check for uppercase or lowercase 'S'.


  LOOP AT itab1.
    CLEAR: s_len, p_center.
    s_len = strlen( itab1-ptr1 ) - 1.
    p_center = itab1-ptr1+s_len(1).
    IF p_center EQ 'S'.
      DELETE itab1.
      CONTINUE.
    ELSE.
*-- do something
    ENDIF.
  ENDLOOP.

Also, if you are sure that your field PTR1 will be filled to the last character, then you can do this with a single statement. Let us your field PTR1 is of length 10 and all 10 characters will always be filled in. So the last character has to be the 10th character. In that case, you can use the following single statement.

DELETE itab1 WHERE ptr1+9(1) = 'S'.

Hope this helps.

Srinivas

0 Kudos

Try this :


DATA : W_lentgh        type n.
FIELD-SYMBOLS <OFFSET> TYPE ANY.


loop at itab1.
w_lenght_1 = strlen( ITAB1-PTR1 ) - 1.

ASSIGN W_lentgh_1 TO <OFFSET>.
      IF ITAB1-PTR1+<OFFSET>(1) CO 'S'
   or IF ITAB1-PTR1+<OFFSET>(1) CO 's' .
        delete itab1.
      ENDIF.

endloop.

0 Kudos

Is there a way to use '%s' instead of using strlen and using a varialbe to store the value of the strlen and comparing it in the IF statement.

0 Kudos

Hi Sandhya,

What you are looking for is the CP operator. Here's what I believe to be the most elegant solution to your problem -

loop at itab1 where PTR1 <b>cp</b> '*s'.
  delete itab1.
endloop.

Regards,

Anand Mandalika.

0 Kudos

Is your field have a fixed lentgh ?

I think no.

And moreover you can't use '%s' in a loop.

Using % means that you use the 'like' statement only available in a 'where clause' of a 'Select'.

CO 'S' --> Contains Only

CA 'S' --> Contains Any

Why don't you want to use what I've posted above.

I think it works well !

Regards,

Erwan.

0 Kudos

Or even better,

DELETE ITAB1 WHERE PTR1 CP '*s'.

Of course, the operation CP is going to be case-sensitive.

0 Kudos

Hi,

Just try this.

No need of loop.

delete itab1 where ptr1 cp 'S' or ptr1 cp 's'.

For more information about CP , just have a look on this.

The logical expression

<f1> CP <f2>

is true if <f1> matches the pattern <f2>. If <f2> is of type C, you can use the following wildcards in <f2>:

• for any character string: *

• for any single character: +

Trailing spaces are ignored and the comparison is not case-sensitive. If the comparison is true, the system field SY-FDPOS contains the offset of <f2> in <f1> . If it is false, SY-FDPOS contains the length of <f1>.

If you want to perform a comparison on a particular character in <f2>, place the escape character # in front of it. You can use the escape character # to specify

• characters in upper and lower case

• the wildcard character "" (enter:#)

• the wildcard character "" (enter: # )

• the escape symbol itself (enter: ## )

• blanks at the end of a string (enter: #___ )

Message was edited by: Jayanthi Jayaraman

0 Kudos

Hi,

I'm interested in knowing how that's different from what I have posted...

Regards,

Anand Mandalika.

0 Kudos

Sorry I didn't check yours. After posting only, I checked.

0 Kudos

Hi Sandhya,

do you mean this ( joker: * instead of %-by select)

  LOOP AT itab1.
    IF itab1-butxt CP '*S'
    OR itab1-butxt CP '*s'.
*      DELETE itab1 INDEX sy-tabix.
      WRITE : / itab1-butxt.

      CONTINUE.
    ELSE.
      CONTINUE.
    ENDIF.
  ENDLOOP.

so your statemanet must be <b>cp '*S'</b>

regards Andreas

it's unbelievable - so much postings in 15 minutes

-> but Anand was faster

Message was edited by: Andreas Mann

0 Kudos

Anand's seems the best solution. Thanks Anand, you have also helped me.

0 Kudos

It's my pleasure.