Hi,
I have an requirment where i need to subtract months from the given date yyyymm,
can you please suggest me to achieve this without using any routine.
Regards,
Aditya
Subtracting months from given date
Moderators: chulett, rschirm, roy
-
- Premium Member
- Posts: 104
- Joined: Thu Jul 12, 2007 11:32 pm
- Location: Canada
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
Why did you post in the server forum? Is there a maximum number of months? If so, a couple of stage variables, some If..Then..Else logic and some arithmetic should see you through.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
-
- Premium Member
- Posts: 104
- Joined: Thu Jul 12, 2007 11:32 pm
- Location: Canada
If the phrase "subtract months" is in a set pattern against the "yyyymm" format shown, then a fraction-of-12 calculation becomes easy to do.
The year is the integer portion, and your mm value is divided by 12 to convert it to decimal. Convert the number of months to be subtracted to decimal, do the subtraction (works just as well for addition) and change the decimal fraction back to its month equivalent.
You don't have worry about crossing the century year, since it is treated as an ordinary integer value.
You can, if you want to maintain precision without worrying about rounding, keep 12 stage variables with the decimal equivalent for each month. One month is 0.08333. Your table can be your "translation" of the month number to its decimal and back again.
Please note: You have to adjust for the fact that December divided by 12 is 1. That's why I described using a lookup "table" with stage variables.
In my days as an actuary, in the dinosaur age using Lotus 1-2-3 on dual-floppy IBM PCs, we had a standard and "coded" for it very easily.
The year is the integer portion, and your mm value is divided by 12 to convert it to decimal. Convert the number of months to be subtracted to decimal, do the subtraction (works just as well for addition) and change the decimal fraction back to its month equivalent.
You don't have worry about crossing the century year, since it is treated as an ordinary integer value.
You can, if you want to maintain precision without worrying about rounding, keep 12 stage variables with the decimal equivalent for each month. One month is 0.08333. Your table can be your "translation" of the month number to its decimal and back again.
Please note: You have to adjust for the fact that December divided by 12 is 1. That's why I described using a lookup "table" with stage variables.
In my days as an actuary, in the dinosaur age using Lotus 1-2-3 on dual-floppy IBM PCs, we had a standard and "coded" for it very easily.
Franklin Evans
"Shared pain is lessened, shared joy increased. Thus do we refute entropy." -- Spider Robinson
Using mainframe data FAQ: viewtopic.php?t=143596 Using CFF FAQ: viewtopic.php?t=157872
"Shared pain is lessened, shared joy increased. Thus do we refute entropy." -- Spider Robinson
Using mainframe data FAQ: viewtopic.php?t=143596 Using CFF FAQ: viewtopic.php?t=157872