Skip to Content

How can I compare one date range with another date range?

I want to compare to date and from date from one internal table.

ex: from to

2-1-2013 5-1-2013

6-1-2013 8-1-2013

11-1-2013 15-1-2013

9-1-2013 10-1-2013

I want to compare fst row with 2nd and 3rd row.

Ideally," From " date should be less than To date. and I want to display wrong entries, means if "to" date is 8-1-2013, its next "From" date should be 9-1-2013. in above ex, its 11-1-2013 which means its wrong. but in last row there is correct entry. so I want to check if in the same internal table correct entries exist or not.

So, I want to compare ranges, and display wrong entries.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Posted on Dec 30, 2013 at 10:13 AM

    Hello Sheetal,

    In SAP, date store in YYYYMMDD format means 15-1-2013 Visible in output will be stored as 20130115.. So you can use any comparison operator like " gt, lt, eq'.

    Take the following code as a demo code. Syntax error can be there

    LOOP AT itab INTO wa.
    READ TABLE itab INTO wa1 INDEX sy-tabix + 1.
    CLEAR sy-tabix.
    IF sy-subrc = 0.
    IF wa-to-date + 1 = wa1-from_date.
    " Next entry is correct.
    ELSE.
    READ TABLE itab INTO wa1 WITH KEY from_date = wa-to_date + 1.
    IF sy-subrc = 0.
    "'Entry exist'.
    ELSE.
    "Entry not exist'.
    ENDIF.
    ENDIF.
    ELSE.
    "No correct entry.
    ENDIF.
    ENDLOOP..

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Dec 30, 2013 at 10:18 AM

    Hello Sheetal.

    Assuming your date fields in internal table are declared properly (TYPE DATS),

    Sort the internal table by date field and perform your business logic check.

    For instance,

    DATA: tmp_date TYPE DATS.

    loop at itab.

    if tmp_date is initial.

    tmp_date = itab-to_date + 1.

    elseif itab-from_date <> tmp_date.

    MESSAGE 'Incorrect from date!' TYPE 'S' DISPLAY LIKE 'E'.

    leave to screen sy-dynnr.

    else.

    tmp_date = itab-to_date + 1.

    endif.

    endloop.

    Regards.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Dec 30, 2013 at 10:46 AM

    Hi sheetal

    Kindly check the follwing..

    tables : ztb_test.

    data : itab type table of ztb_test.
    data : gwa type ztb_test.
    data : gwa1 type ztb_test.
    data : tdate type datum.

    select * from ztb_test into table itab.

    (I have created a table with ur inputs where the last entry is with wrong date.)



    sort itab by dfrom.

    LOOP AT itab into gwa.
    IF gwa-dfrom < gwa-dto.
    else.
    MESSAGE 'from date is greater than to date' type 'E'.
    ENDIF.
    read table itab into gwa1 index sy-tabix + 1.
    IF sy-subrc = 0.
    clear tdate.
    tdate = gwa-dto + 1.
    IF gwa1-dfrom = tdate.
    else.
    message 'next entry is wrong.. after the date ' type 'E'.
    ENDIF.
    clear gwa1.
    ENDIF.
    clear gwa.
    ENDLOOP.


    Regards,

    Sindhuja


    Capture.JPG (19.2 kB)
    Capture1.JPG (21.2 kB)
    Add a comment
    10|10000 characters needed characters exceeded

    • Hello Sindhuja,

      B is not wrong entry, but the requirement is I need to show wrong entries with concern to its previous entry. C is wrong one, how I am telling it as wrong, because I am comparing it with B. So requirement is I need to show wrong entry, and its previous correct entry with which I am confirming it as wrong.

      Regards,

      Sheetal

  • Posted on Dec 30, 2013 at 12:32 PM


    Hi Sheetal,

    Fromdate todate
    02-1-2013 05-1-2013
    06-1-2013 08-1-2013
    11-1-2013 15-1-2013
    09-1-2013 10-1-2013

    clear: lv_count , lv_sy_tabix.

    describe table it_itab Lines lv_count.
    lv_count = lv_count - 1.
    lv_sy_tabix = lv_sy_tabix + 1.

    read table it_tab with index 1 into wa_itab.
    if sy-subrc = 0.

    lv_sy_tabix = lv_sy_tabix + 1. "to read from 2nd record onwards
    do lv_count times.

    read table it_itab with index lv_sy_tabix into wa_itab2.
    if sy-subrc = 0.
    if Wa_itab-fromdt BETWEEN Wa_itab2-fromdate AND Wa_itab2-todate.
    lv_found = 'Found'.
    write:/Wa_itab2-fromdate , Wa_itab2-todate.

    Else.
    lv_found = 'Not found in Range entries'.
    endif.

    IF Wa_itab2-fromdt LT Wa_itab-fromdt.

    lv_found = 'Not found in Range entries'.

    ELSE.

    lv_found = 'Found'.

    write:/Wa_itab2-fromdate , Wa_itab2-todate.

    endif.

    IF Wa_itab2-todt LT Wa_itab2-fromdt.

    lv_found = 'Not found in Range entries'.

    ELSE.

    lv_found = 'Found'.

    write:/Wa_itab2-fromdate , Wa_itab2-todate.

    endif.

    enddo.

    endif.

    Thanks,

    Nelson Karunakar Darla

    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.