Application Development 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: 

Split dates

Former Member
0 Kudos
856

Hi experts,


I need function module to split a table of date intervals,  like picture below:

in the result internal table like picture below:

Regards,

André Nunes.

1 ACCEPTED SOLUTION

former_member184569
Active Contributor
0 Kudos
234

Try this logic.


Your input (in_tab) and output table (out_tab) would be the structure given above (workarea - wa_tab).


There would be an intermediate table sort_tab (work area wa_sort) with just one date field.

I am using two field symbols <c_sort> and <p_sort> to access the current and previous contents of sort table.


types : begin of w_tab,
    dat type datum,
    end of w_tab.

data : sort_tab type TABLE OF w_tab,
           wa_sort type w_tab.
FIELD-SYMBOLS : <p_sort> type w_tab,
                 <c_sort> type w_tab.



* First move all the dates in the input table to the sort table.

    clear wa_tab.
    loop at in_tab into wa_tab.
      wa_sort-dat = wa_tab-begda.
      append wa_sort to sort_tab.
      clear wa_sort.
      wa_sort-dat = wa_tab-endda.
      append wa_sort to sort_tab.
      clear : wa_sort, wa_tab.
   endloop.

* Sort sort_tab.
   sort sort_tab by dat.

* Now in pairs add the contents to the output table.


   loop at sort_tab ASSIGNING <c_sort>.
     if sy-tabix = 1.
       ASSIGN <c_sort> to <p_sort>.
       CONTINUE.
     endif.
     wa_tab-begda = <p_sort>-dat.
     wa_tab-endda = <c_sort>-dat.
     append wa_tab to out_tab.
     ASSIGN <c_sort> to <p_sort>.
   endloop.



8 REPLIES 8

ceedee666
Active Contributor
0 Kudos
234

Hi Andre,

i don't know of a standard function module or method to achieve this. However, you can write one fairly easy yourself. What you need to do is:

  1. Collect all dates (start and end dates) in one table consisting just of one date field.
  2. sort this table ascending
  3. delete adjacent duplicates
  4. build the date intervals by using the 1st and 2nd entry for the first interval, 2nd and 3rd for the second interval and so forth

Best,

Christian

former_member221372
Participant
0 Kudos
234

Hi,

Sort the internal table.

Former Member
0 Kudos
234

Hi Andre,

  You can apply this logic:

STEP 1.  SORT the date table based on START DATE.

STEP 2. LOOP at sorted date table.

STEP 3. Store first entry of date table into result table

STEP 4. READ inside the same LOOP. Logic will be START DATE of current line will be LAST date of privious line. Store the entry in result table.

Hope its clear. Let me know if you need more input.

BR,

Prakash

former_member184569
Active Contributor
0 Kudos
235

Try this logic.


Your input (in_tab) and output table (out_tab) would be the structure given above (workarea - wa_tab).


There would be an intermediate table sort_tab (work area wa_sort) with just one date field.

I am using two field symbols <c_sort> and <p_sort> to access the current and previous contents of sort table.


types : begin of w_tab,
    dat type datum,
    end of w_tab.

data : sort_tab type TABLE OF w_tab,
           wa_sort type w_tab.
FIELD-SYMBOLS : <p_sort> type w_tab,
                 <c_sort> type w_tab.



* First move all the dates in the input table to the sort table.

    clear wa_tab.
    loop at in_tab into wa_tab.
      wa_sort-dat = wa_tab-begda.
      append wa_sort to sort_tab.
      clear wa_sort.
      wa_sort-dat = wa_tab-endda.
      append wa_sort to sort_tab.
      clear : wa_sort, wa_tab.
   endloop.

* Sort sort_tab.
   sort sort_tab by dat.

* Now in pairs add the contents to the output table.


   loop at sort_tab ASSIGNING <c_sort>.
     if sy-tabix = 1.
       ASSIGN <c_sort> to <p_sort>.
       CONTINUE.
     endif.
     wa_tab-begda = <p_sort>-dat.
     wa_tab-endda = <c_sort>-dat.
     append wa_tab to out_tab.
     ASSIGN <c_sort> to <p_sort>.
   endloop.



0 Kudos
234

Thanks Susmitha.

Former Member
0 Kudos
234

Hello,

Where come those dates in the first place?

Was there a merge already of several tables records to build that input table?

If yes, the output could also be reached with the statement PROVIDE on the internal tables with unique intervals. (just for info).

Br,

Manu.

Former Member
0 Kudos
234

The date slice class should be helpful. Read my reply here.

Former Member
0 Kudos
234

Thanks all.