Application Development and Automation 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: 
Read only

Issue with the read statement

Former Member
0 Likes
2,597

Hello All,

I have an internal table with one field that contains the name of all the customers and the name can be in lower case, upper case or a mix of both, now I need to read this table into another table for all the names those have the word 'DEPARTMENT'. So if there are entries in the internal table like IT_DEPARTMENT or it_department or It_Department then all these should show up when I read my internal table.

I tried the below code but it's not correct :

read table it_data into wa_data with TABLE KEY name CO 'DEPARTMENT'

if sy-subrc = 0.

   append wa_data to it_final_data.

  CLEAR wa_data.

endif.

can you please help me this.

Thanks.


1 ACCEPTED SOLUTION
Read only

former_member585060
Active Contributor
0 Likes
2,086

Hi,

    Try to loop in it_data and append the matching records which has DEPARTMENT in customer field using Contains Pattern(CP).

Ex:-

TYPES : BEGIN OF ty_itab,
                 kunnr TYPE c LENGTH 20,
               END OF ty_itab.

DATA : i_itab    TYPE STANDARD TABLE OF ty_itab,
           i_itab1   TYPE STANDARD TABLE OF ty_itab,
           wa_itab TYPE ty_itab,
           v_kunnr TYPE c LENGTH 20.


wa_itab-kunnr = 'it_department'.
APPEND wa_itab TO i_itab.

wa_itab-kunnr = 'It_Department'.
APPEND wa_itab TO i_itab.

wa_itab-kunnr = 'lt_department'.
APPEND wa_itab TO i_itab.

wa_itab-kunnr = 'Nt_department'.
APPEND wa_itab TO i_itab.

wa_itab-kunnr = 'Ot_department'.
APPEND wa_itab TO i_itab.

wa_itab-kunnr = 'Bala Krishna'.
APPEND wa_itab TO i_itab.

SORT i_itab.

LOOP AT i_itab INTO wa_itab.

  v_kunnr = wa_itab-kunnr.

  TRANSLATE v_kunnr TO UPPER CASE.

  IF v_kunnr CP '*DEPARTMENT'.  
    APPEND wa_itab TO i_itab1.    " you will have only records with customer with DEPARTMENT
  ENDIF.

  CLEAR : v_kunnr, wa_itab.
ENDLOOP.

*Note If for some customers other texts exists after 'Department' use '*DEPARTMENT*', just click F1 on CP option, it will list you all the available options on that.

Thanks & Regards

Bala Krishna

12 REPLIES 12
Read only

former_member585060
Active Contributor
0 Likes
2,087

Hi,

    Try to loop in it_data and append the matching records which has DEPARTMENT in customer field using Contains Pattern(CP).

Ex:-

TYPES : BEGIN OF ty_itab,
                 kunnr TYPE c LENGTH 20,
               END OF ty_itab.

DATA : i_itab    TYPE STANDARD TABLE OF ty_itab,
           i_itab1   TYPE STANDARD TABLE OF ty_itab,
           wa_itab TYPE ty_itab,
           v_kunnr TYPE c LENGTH 20.


wa_itab-kunnr = 'it_department'.
APPEND wa_itab TO i_itab.

wa_itab-kunnr = 'It_Department'.
APPEND wa_itab TO i_itab.

wa_itab-kunnr = 'lt_department'.
APPEND wa_itab TO i_itab.

wa_itab-kunnr = 'Nt_department'.
APPEND wa_itab TO i_itab.

wa_itab-kunnr = 'Ot_department'.
APPEND wa_itab TO i_itab.

wa_itab-kunnr = 'Bala Krishna'.
APPEND wa_itab TO i_itab.

SORT i_itab.

LOOP AT i_itab INTO wa_itab.

  v_kunnr = wa_itab-kunnr.

  TRANSLATE v_kunnr TO UPPER CASE.

  IF v_kunnr CP '*DEPARTMENT'.  
    APPEND wa_itab TO i_itab1.    " you will have only records with customer with DEPARTMENT
  ENDIF.

  CLEAR : v_kunnr, wa_itab.
ENDLOOP.

*Note If for some customers other texts exists after 'Department' use '*DEPARTMENT*', just click F1 on CP option, it will list you all the available options on that.

Thanks & Regards

Bala Krishna

Read only

0 Likes
2,086

Hi Bala,

Thanks a lot for the reply and it works. I I have another can I do something same with the select statement. Can I select the data from table KNA1...and the scenario is still the same that I listed above.

Read only

0 Likes
2,086

Hi,

    Yes, you can use LIKE operator in WHERE clause.

SELECT kunnr

                       FROM kna1

                       INTO TABLE i_itab

                       WHERE kunnr LIKE '%115'.

SAP help on Select

 

Comparing Strings

 

To find out whether the value of a column matches a pattern, use:

 

SELECT ... WHERE <s> [NOT ] LIKE <f> [ESCAPE <h>] ...

The condition is true if the value of the column <s> matches [does not match] the pattern in the data object <f>. You can only use this test for text fields. The data type of the column must be alphanumeric. <f> must have data type C.

You can use the following wildcard characters in <f>:

  • % for a sequence of any characters (including spaces).
  • _ for a single character.

 

For example, ABC_EFG% matches the strings ABCxEFGxyz and ABCxEFG, but not ABCEFGxyz. If you want to use the two wildcard characters explicitly in the comparison, use the ESCAPE option. ESCAPE <h> specifies an escape symbol <h>. If preceded by <h>, the wildcards and the escape symbol itself lose their usual function within the pattern <f>. The use of _ and % corresponds to Standard SQL usage. Logical expressions elsewhere in ABAP use other wildcard characters (+ and *).

Thanks & Regards

Bala Krishna

Read only

0 Likes
2,086

You can use

SELECT name1 from Kna1 into table lt_kna1 where name CS 'department'.

The CS operator is not Case sensitive.

Read only

0 Likes
2,086

When I tried using operator CS I got this error message:

'CS' is not a valid comparison operator

Read only

0 Likes
2,086

You cannot use 'CS' or 'CP' etc in SELECT statements, have you tries with LIKE statement which i gave in my last post.

Read only

0 Likes
2,086

Hi,

Sorry for misleading.

We cannot use CS and I dont beleive LIKE will solve your issue here.

LIKE is case sensitive.

You can use

SELECT name1 from Kna1 into table lt_kna1 where name1 LIKE '%department%' or name1 like '%Department%'.  IF these are the only two combinations.

If you have multiple combinations like DePartMent, then this will not work.

You can use native sql to solve this or select all the entries and then eliminate the entries as told in the first question.

Check some posts on Case insensitive select queries.

http://scn.sap.com/thread/1626987

Read only

0 Likes
2,086

Depending on your database, you may be able to use native SQL to SELECT data regardless of case.

Rob

Read only

0 Likes
2,086

If you need to match more strict format checking you can use find instead, with regex and ignoring case addition.

Regular Expressions are just too powerful.

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
2,086

For time being convert the internal table value to upper case and also the comparing value to upper case. Then loop and check it using CP *DEPARTMENT*

Read only

Former Member
0 Likes
2,086

in select queries you can't use, CP CS CO etc. just translate everything in the internal table to upper case and then check for the pattern. If that matches the pattern move it to the another table.

Read only

Former Member
0 Likes
2,086

Why dont you Try 'SEARCH'. Thanks