Suppose in a table say “MAKT” there is a field “MAKTX (Description of Material) which is of length 40 characters. This field may contain values of different lengths say some values may be 25 char long and some 30 char long, etc.
Now, there is a requirement to select those records where the length of MAKTX field is 25 CHAR i.e. selection based on MAKTX field in which the values are exactly 25 CHAR in length.
What you can do is select all records then loop on internal table find length of each value for that field and then delete other records as shown below:-
SELECT * FROM MAKT
UPTO 10 ROWS
WHERE spras = ‘EN’.
Loop at gi_makt into gw_makt.
Lv_len = strlen (gw_makt-maktx).
IF lv_len <> 25.
Delete gi_makt .
Now, you can select records with required field value length directly with following query.
SELECT * FROM makt INTO TABLE gi_makt
UP TO 10 ROWS
WHERE spras = ‘EN’ AND
maktx LIKE ‘_________________________’.
- Here MAKTX is given as 25 underscores ‘_’. If you run this query it will select all the records with 25 length description.
There can also be a requirement wherein you need to select only those values from a table say “T001W” where 1st character of a field e.g. “WERKS” (Plant) is “1”.
In such case also u can use “LIKE” as shown in the below sample code.
DATA : gi_t001w TYPE STANDARD TABLE OF t001w,
gw_t001w TYPE t001w.
SELECT * FROM t001w
INTO TABLE gi_t001w
UP TO 10 ROWS
WHERE werks like ‘1%‘.
LOOP AT gi_t001w INTO gw_t001w.
WRITE :/ gw_t001w-werks.
Output :- It will give all the plants starting with “1”.
Similarly you can use this to select those records where a field contains a particular string or based on last letter of the field, etc as per the requirement.