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

Date fetching from table

Former Member
0 Likes
800

I have a customized table which contains monthwise details.say 12 entries for an year.I want to fetch the previous 5 month entries from the table wrto sy-datum.

ie) if the month is 08 , then i want the records frm 03-07.

ie) if the month is 02, i want the records 01,12,11,10,09.

What logic to use?

Rgds,

SAPUSER100

1 ACCEPTED SOLUTION
Read only

naimesh_patel
Active Contributor
0 Likes
765

Hello,

ranges: r_mon for monat.

l_month = sy_datum+4(2) - 5.

do 5 times.

r_mon-sign = 'I'.

r_mon-option = 'EQ'.

r_mon-low = l_month.

append r_mon.

l_mont = l_month + 1.

if l_month > 12.

l_month = 1.

endif.

enddo.

regards,

Naimesh

6 REPLIES 6
Read only

naimesh_patel
Active Contributor
0 Likes
766

Hello,

ranges: r_mon for monat.

l_month = sy_datum+4(2) - 5.

do 5 times.

r_mon-sign = 'I'.

r_mon-option = 'EQ'.

r_mon-low = l_month.

append r_mon.

l_mont = l_month + 1.

if l_month > 12.

l_month = 1.

endif.

enddo.

regards,

Naimesh

Read only

0 Likes
765

hi ,

how you fetch it from an itab ?

hav to use READ st by comparing two tables or any direct way is possible ?

Read only

0 Likes
765

Hello,

You have to also consider the year in your selection. so, change in the logic ...

data: begin of r_mon occurs 0,

mon(2),

year(4),

end of r_mon.

l_month = sy_datum+4(2) - 1.

l_year = sy_datum+0(4).

do 5 times.

r_mon-mon = l_month.

r_mon-year = l_year.

append r_mon.

clear r_mon

l_mont = l_month - 1.

if l_month = 0 .

l_month = 12.

l_year = sy_datum+0(4) - 1.

endif.

enddo.

Once you got filled your range of the Month,

I would advice like,

loop at r_mon.

select * from ztab

appending table itab

where mon = r_mon-mon

and year = r_mon-year.

endloop.

Regards,

Naimesh

Read only

0 Likes
765

Hi everyone,

just a tip: I've worked a lot with date manipulation and I had a lot of tiny problems when doing so manually, you know month 13 or month 00...

Then my lazyness wakes up and then I think: "let another one do the job for you". So if you use function module ADD_TIME_TO_DATE, you'll avoid some headaches.

Best regards,

Alvaro

Read only

Former Member
0 Likes
765

Hi

Just try out this function module.Using this you can solve your problem,i guess. Execute it in SE37 and see if it is giving the required result for you.

CCM_GO_BACK_MONTHS

Regards,

Vijay Penumaka

Read only

Former Member
0 Likes
765

even you can check this function module,

/BEV4/PLPS__ADD_MONTH_TO_DATE

say -5 in months & give data is current date.

this works fine.

regards

srikanth