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

fetching nearest date based on input date

Former Member
0 Likes
1,071

Hi,

I want to fetch nearest date whith given input date, Please let me know how to do this?

If i pass 06/11/2005 , I should get 6/13/2005

If i pass 04/20/2005 , I should get 4/13/2005

01/12/2005

02/10/2005

03/14/2005

04/13/2005

05/12/2005

06/13/2005

07/13/2005

08/11/2005

09/13/2005

10/12/2005

11/10/2005

12/13/2005

01/12/2006

Thanks,

sonar

3 REPLIES 3
Read only

kesavadas_thekkillath
Active Contributor
0 Likes
584

add a field diff to the internal table.

before processing convert the date in itab and the input to internal format.



loop at itab into wa.
wa-diff = abs( wa-date - lv_date ). "Here lv_date is your input
modify itab index sy-tabix from wa  transporting diff.
endloop.

sort itab ascending.
read  table itab into wa index 1 transporting date.
if sy-subrc = 0.
write wa-date.
endif.

Read only

Former Member
0 Likes
584

Hi,

what you can do is split entire data into 2 internal tables i.e.

itab1 - where DATE >= input_date ; then sort itab1 by DATE ascending and delete adjacent duplicates, read itab1 index 1

itab2 - where DATE <= input_date ; then sort itab2 by DATE descending and delete adjacent duplicates, read itab2 index 1

now get difference of itab1-DATE and input_date, itab2-DATE and input_date, whichever is smaller is the closest date

do all this in a perform by passing main table and input date

Note: If you do not want strucure of main internal tbale to be disturbed, above logic can be used

Regards

Vasu

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
584

Hello Sonar,

Does not look that difficult

SORT ITAB BY DATE ASCENDING.

LOOP AT ITAB INTO WA.

IF WA-DATE > V_DT_INPUT.
V_INDEX = SY-INDEX - 1.

READ TABLE ITAB INTO WA1 INDEX V_INDEX.
IF SY-SUBRC = 0.
V_DT_OUTPUT = WA1-DATE.
ENDIF.

ENDIF.

ENDLOOP.

Hope i am clear.

BR,

Suhas