Skip to Content
author's profile photo Former Member
Former Member

Using LIKE in SELECT ..... WHERE

Hi,

I need to do pattern matching during SELECT so that a table field contains or is the same as an input variable. How can I accomplish this?

Regards,

Nanditha

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Best Answer
    Posted on Jun 01, 2005 at 07:44 PM

    Here is the "help" for the syntax of LIKE in SELECT statement.

    <i>

    f [NOT] LIKE g

    Addition:

    ... ESCAPE h

    Effect

    The condition is met for a table entry if the statement "f (does not) equal the pattern in g" is true for the values of f and g. f must always be a field descriptor, and g an ABAP field. If f has the value NULL, then the result of the check for the statement is unknown. Within a pattern, there are two special characters:

    '_' (underscore) stands for any single character.

    '%' (percentage sign) stands for any sequence of characters, including an empty string.

    Examples

    Example to select all customers whose name begins with 'M':

    TABLES SCUSTOM.

    SELECT ID NAME FROM SCUSTOM

    INTO CORRESPONDING FIELDS OF SCUSTOM

    WHERE NAME LIKE 'M%'.

    WRITE: / SCUSTOM-ID, SCUSTOM-NAME.

    ENDSELECT.

    Example to select all customers whose name contains 'huber':

    TABLES SCUSTOM.

    SELECT ID NAME FROM SCUSTOM

    INTO CORRESPONDING FIELDS OF SCUSTOM

    WHERE NAME LIKE '%huber%'.

    WRITE: / SCUSTOM-ID, SCUSTOM-NAME.

    ENDSELECT.

    Example to select all customers whose name does not contain 'n' as the second character:

    TABLES SCUSTOM.

    SELECT ID NAME FROM SCUSTOM

    INTO CORRESPONDING FIELDS OF SCUSTOM

    WHERE NAME NOT LIKE '_n%'.

    WRITE: / SCUSTOM-ID, SCUSTOM-NAME.

    ENDSELECT.

    Notes

    LIKE can only be used for alphanumeric database fields. In other words, table field f must have Dictionary type ACCP, CHAR, CLNT, CUKY, LCHR, NUMC, UNIT, VARC, TIMS or DATS. The comparison field g must always have type C.

    The maximum length of the pattern is 2n - 1 characters, where n is the length of field f.

    Trailing spaces are ignored in comparison field g. If a pattern contains trailing spaces, you must enclose it in single inverted commas ('). If your pattern is enclosed in inverted commas and you also want to include inverted commas as part of the pattern, the inverted commas in the pattern must be doubled.

    You cannot use this variant in the ON addition to the FROM clause.

    </i>

    Regards,

    Rich Heilman

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 01, 2005 at 07:41 PM

    You can use SELECT-OPTIONS for your select screen.

    Then in your select statement use the IN operator.

    
    data: imakt type table of makt with header line.
    
    select-options: s_maktx for imakt-maktx.
    
    select * from makt
             into corresponding fields of table imakt
                       where maktx in s_maktx.
    
    

    or you can "hardcode" the patterns

    
    * This code selects all material groups that start with 
    * DRA or DWA
    
    data: begin of i_matkl occurs 0,
          matkl type mara-matkl.
    data: end of i_matkl.
    
      select matkl from  t023
              into corresponding fields of table i_matkl
                      where ( matkl like 'DRA%'
                        or matkl like 'DWA%' ).
    
    

    Regards,

    Rich Heilman

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 01, 2005 at 07:42 PM

    Hi Nanditha

    You can use LIKE as follows:

    SELECT * FROM mara
           INTO TABLE lt_mara
           WHERE matnr LIKE '%AD%' .

    Regards

    *--Serdar https://www.sdn.sap.com:443http://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.sdn.businesscard.sdnbusinesscard?u=qbk%2bsag%2bjiw%3d">[ BC ]

    -


    Rich, you are here again 😊

    Message was edited by: Serdar Simsekler

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jun 01, 2005 at 07:43 PM

    Example to select all customers whose name begins with 'M':

    SELECT ID NAME FROM SCUSTOM

    INTO CORRESPONDING FIELDS OF SCUSTOM_WA

    WHERE NAME LIKE 'M%'.

    you can find more examples under :

    Logical Condition in Database Access Statements.

    May be this is what you were looking for.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.