Subtracting Months

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
jeredleo
Participant
Posts: 74
Joined: Thu Jun 19, 2003 8:49 am

Subtracting Months

Post by jeredleo »

I am trying to figure out the best way to subtract months from a date but still keep the day.

For Example 10-23-2002 (looking for 3 months prior)
Result: 07-23-2002

Example 02-12-2002 (looking for 6 months prior)
Result: 08-12-2001

Please let me know if anyone has a good solution, I also need to keep the format as above with leading zeros.

Thanks
WoMaWil
Participant
Posts: 482
Joined: Thu Mar 13, 2003 7:17 am
Location: Amsterdam

Post by WoMaWil »

Hi,

For this you could write a little Routine with 2 Parameters the first for the date and the second for the number of Month:

First you have to be sure that your dateformat is allways like this.

The you divide your string up:
MyDay=MyDate[4,2]
MyMonth=MyDate[1,2]
MyYear=MyDate[4]

Then you have to analyse the number of month you want to substract:

You divide it by twelfe and you need even the rest for to get the right year

Then you make your calculation.

And in the end you put the New Parts together.

That's all and it is easy!

Wolfgang


I am trying to figure out the best way to subtract months from a date but still keep the day.

For Example 10-23-2002 (looking for 3 months prior)
Result: 07-23-2002

Example 02-12-2002 (looking for 6 months prior)
Result: 08-12-2001

Please let me know if anyone has a good solution, I also need to keep the format as above with leading zeros.

Thanks


chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

This works fine as long as your day is not greater than 28. If it is, then you will end up creating dates like Feb 3oth or June 31st. Too bad Ascential doesn't provide routines to do this for us.

I've done this in the past (in other languages) by building an array of 'month end days' and then checking to see if you exceed them and adjusting the date if you have. 'Course you have to worry about leap years as well if you want the routine to stand the test of time.

Perhaps if someone does write (or has written) a good date routine to do month (and year) addition/subtraction, they could share it with the group.

-craig
Post Reply