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

Getting a string from a table using a SELECT statement

Former Member
0 Likes
5,310

Hi experts,

I am using this line of code when choosing from table LTDXT.

     SELECT variant

       FROM ltdxt

       INTO CORRESPONDING FIELDS OF TABLE ts_variant

      WHERE report EQ sy-repid

        AND  ( username EQ space

         OR username EQ sy-uname )

        AND  ( variant LIKE '%_A%'                      

         OR    variant LIKE '%A_%' ).

What I want to appear inside the table should be variants that either contain "_A" or "A_" on the name. But when I check the table, even the variant name ABTEST is appearing on the table even if it does not contain A_ or _A.

Am I missing something? Thank you!

1 ACCEPTED SOLUTION
Read only

PeterJonker
Active Contributor
0 Likes
3,266

it is expected. abap help on like:

"%" represents any character string, even an empty one, and "_" represents any character

if I read further in the ABAP help it says on wildcards:

Character that substitutes other content. In logical expressions with the
operators CP und NP, "*" usually stands for any character
string
and "+" for any
individual character. In WHERE-conditions of Open SQL with the operator LIKE "%" stands for any character strings and "+" for individual characters. The special
functions of wildcard characters can be canceled using escape symbols

7 REPLIES 7
Read only

PeterJonker
Active Contributor
0 Likes
3,267

it is expected. abap help on like:

"%" represents any character string, even an empty one, and "_" represents any character

if I read further in the ABAP help it says on wildcards:

Character that substitutes other content. In logical expressions with the
operators CP und NP, "*" usually stands for any character
string
and "+" for any
individual character. In WHERE-conditions of Open SQL with the operator LIKE "%" stands for any character strings and "+" for individual characters. The special
functions of wildcard characters can be canceled using escape symbols

Read only

0 Likes
3,266

Peter is correct.  In your case, you can add escape characters as follows and your statement will perform correctly:

SELECT variant

  FROM ltdxt

  INTO CORRESPONDING FIELDS OF TABLE ts_variant

  WHERE report EQ sy-repid

    AND  ( username EQ space

     OR username EQ sy-uname )

    AND  ( variant LIKE '%\_A%' ESCAPE '\'

     OR    variant LIKE '%A\_%' ESCAPE '\' ).

Read only

0 Likes
3,266

Hey I also tried for the same, Its working fine

Great Piece of code.. Thanks for Sharing ..

Read only

Former Member
0 Likes
3,266

In this case, underscore is interpreted as a meaning any single character.  If you use an in instead of a like, you can test for underscore.

Neal

Read only

Former Member
0 Likes
3,266

As said problem with '_' and %.

So do one thing..

SELECT variant  FROM    ltdxt       INTO CORRESPONDING FIELDS OF TABLE ts_variant     

WHERE       report EQ sy-repid        AND 

                    ( username EQ space    OR username EQ sy-uname ).

Now

loop at  ts_variant into ls_variant where variant CS '_A'    OR    variant CS  'A_' .

append ls_variant into ts_var_copy.

endloop.

br

ChanS.

Read only

Former Member
0 Likes
3,266

Hii Dioso,

Either follow the post of

Eric Peterson

or u can do in another way.

select variant and store it into ts_variant  then

filter this internal table according to ur requirement and store it into another internal table.

like,

SELECT variant

       FROM ltdxt

       INTO CORRESPONDING FIELDS OF TABLE ts_variant

      WHERE report EQ sy-repid

        AND  ( username EQ space

         OR username EQ sy-uname ).

if not ts_variant is initial.

    loop at ts_variant into wa_variant where variant CO 'A_' or variant CO '_A'.

          move wa_variant to wa_variant_final.

          append wa_variant_final to ts_variant_final.

          clear wa_variant_final.

     endloop.

endif.

regards

Syed

Read only

Former Member
0 Likes
3,266

Yes, '%' and '_' have other meanings in ABAP. So in order to use them in our select query, we need to provide escape expression as rightly explained by Peter and Eric.


Check these links.

http://www.sapdb.org/7.4/htmhelp/8c/ccce27c71c11d2a97100a0c9449261/content.htm

http://help.sap.com/abapdocu_702/en/abenwhere_logexp_like.htm