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

SUBSTRING in ABAP

Hi Experts,

Can i use the similar SQL statement as below in ABAP SQL or something as it ?

SELECT *

FROM table

where substring (field,1,1) = 'B'

Thanks in advance

Martin

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

13 Answers

  • Posted on Apr 16, 2009 at 08:13 AM

    Hello Martin

    Of course you can use substrings but in a different syntax:

    *&---------------------------------------------------------------------*
    *& Report  ZUS_SDN_DB_SELECT_SUBSTRING
    *&
    *&---------------------------------------------------------------------*
    *& Thread: SUBSTRING in ABAP
    *& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="1309280"></a>
    *&---------------------------------------------------------------------*
    
    REPORT  zus_sdn_db_select_substring.
    
    TABLES: t001.
    
    DATA: gs_t001   TYPE t001.
    
    
    START-OF-SELECTION.
    
      BREAK-POINT.
    
    " NOTE: '_' in SELECT statement means exactly 1 Character
    " NOTE: '%' in SELECT statement means 0 or many Characters
    
    " Select for all company codes having '1' in second place...
      SELECT * FROM t001 INTO gs_t001
        WHERE bukrs like '_1__'.
    
        write: / gs_t001-bukrs.
      ENDSELECT.
      skip.
    
      SELECT * FROM t001 INTO gs_t001
        WHERE bukrs like '_1%'.
    
        write: / gs_t001-bukrs.
      ENDSELECT.
    
    END-OF-SELECTION.
    

    Regards

    Uwe

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Apr 16, 2009 at 08:08 AM

    Hi Martin,

    Substring concept is nothing but the offset conept (Ex: x+2(3)) in ABAP.

    You cannot use it directly in the where condition of an ABAP Statement.

    Instead you need to manipulate it first and then pass it to the select statement.

    Hope this is helpful

    Thanks,

    Babu Kilari

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 16, 2009 at 08:08 AM

    Use the following code as per ur requirement----


    SELECT *
     FROM table
     where field = 'B'.
    
    if field = field+1(2).
    endif.
    endselect.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Apr 16, 2009 at 08:20 AM

    Hello Martin ,

    you can try this way

    select * from table

    where field like 'B%' .

    and you get in the result set all occurrences the starts with 'B' .

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 16, 2009 at 08:12 AM

    Exactly i need this statement for SQl over 2 tables

    e.g.

    WHERE SUBSTRING ( table1field,1,1) = table2field

    Martin

    Add a comment
    10|10000 characters needed characters exceeded

    • Hi Martin,

      As you are using two tables field values you need to take them in to work area first and then manipulate for the specific character.

      Then, you need to pass it on to the Where condition.

      All the above examples suggested might work if you are working with one table.

      Thanks,

      Babu Kilari

  • author's profile photo Former Member
    Former Member
    Posted on Apr 16, 2009 at 08:15 AM

    HI,

    Try this way..

    SELECT *
    FROM table
    WHERE FIELD LIKE '_B%'. ---> check the values which has B in second position.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 16, 2009 at 08:21 AM

    Exactly - > i need run SQL statement over 2 SAP database tables with condition below

    WHERE SUBSTRING ( table1field,1,1) = table2field

    Martin

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 16, 2009 at 08:23 AM

    Hi,

    Yes. You can use substrings in ABAP

    try like this

    select *

    from table

    where field like '_B%'.

    Regards,

    Jyothi CH.

    .

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 16, 2009 at 08:24 AM

    The result - > i CANNOT use this SQL statement (with substring) directly over 2 TABLES

    WHERE SUBSTRING ( table1field,1,1) = table2field

    right ?

    Martin

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 16, 2009 at 09:27 AM

    Thanks Babu

    for your example, but i wanted to use SQL over database tables with something as SUBSTRING

    i have to change logic of the solution , because ABAP doesn't know something as SUBSTRING in SQL

    Martin

    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.