add/minus months function

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
mickboland
Participant
Posts: 27
Joined: Sun Mar 20, 2005 4:23 am
Location: Brisbane, Australia

add/minus months function

Post by mickboland »

Is there a standard function to add or take away months from a date.
Sunshine2323
Charter Member
Charter Member
Posts: 130
Joined: Mon Sep 06, 2004 3:05 am
Location: Dubai,UAE

Post by Sunshine2323 »

Can be done using OCONV, ICONV

Check this post
Warm Regards,
Amruta Bandekar

<b>If A equals success, then the formula is: A = X + Y + Z, X is work. Y is play. Z is keep your mouth shut. </b>
--Albert Einstein
mickboland
Participant
Posts: 27
Joined: Sun Mar 20, 2005 4:23 am
Location: Brisbane, Australia

Post by mickboland »

how?

How would you take 4 months from the date 20/01/06 (ie: return a date of 20/09/05).
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

Your date is coming in as a string in the format dd/mm/yy. You don't care about the dd portion and can do a substring on the month and year and perform you math on those. Even though it could be done as a single line in a derivation, a small routine would be best:

Code: Select all

Routine Subtract4MonthsfromDate(InDate)

InDay = InDate[1,2]
InMonth = InDate[3,2]
InYear = InDate[7,2]

OutMonth = InMonth - 4
IF OutMonth < 1
THEN
   OutMonth = 12 + OutMonth
   OutYear  = OutYear - 1
END
Ans = InDay:'/':OutMonth:'/':OutYear
rleishman
Premium Member
Premium Member
Posts: 252
Joined: Mon Sep 19, 2005 10:28 pm
Location: Melbourne, Australia
Contact:

Post by rleishman »

Don't know how to do it with iconv/oconv alone, but you could write your own routine without too much difficulty.

- Extract 3 numeric vars from your date: YYYY, MM, and DD. You can use iconv/oconv for this - search for "d code" in the online help.
- Add n/12 to YYYY
- Add mod(n, 12) to MM
- If MM>12, YYYY+=1 and MM-=12
- Reassemble date as YYYY-MM-01 and convert to internal date format
- Add DD days.
- If the final date is > MONTH.LAST(YYYY-MM), then set it to MONTH.LAST(YYYY-MM).
Ross Leishman
tcj
Premium Member
Premium Member
Posts: 98
Joined: Tue Sep 07, 2004 6:57 pm
Location: QLD, Australia
Contact:

Post by tcj »

Don't forget to check for leap year :D
rleishman
Premium Member
Premium Member
Posts: 252
Joined: Mon Sep 19, 2005 10:28 pm
Location: Melbourne, Australia
Contact:

Post by rleishman »

Doesn't MONTH.LAST do leap-year checking?
Ross Leishman
tcj
Premium Member
Premium Member
Posts: 98
Joined: Tue Sep 07, 2004 6:57 pm
Location: QLD, Australia
Contact:

Post by tcj »

Yeah it does.

My post was for ArndW.
rasi
Participant
Posts: 464
Joined: Fri Oct 25, 2002 1:33 am
Location: Australia, Sydney

Post by rasi »

Hi mick

How would you take 4 months from the date 20/01/06 (ie: return a date of 20/09/05).
You cannot simply substract 4 months from the given date always. Because value "30/06/05 " will give you 30/02/05 which is not a valid date. You have to consider day part as well for the above value.

It's always a good practice to check whether the returned value is valid date or not. Have a business logic defined for cases like this either you take the last day of the month or first day of next month etc.

Thanks
Regards
Siva

Listening to the Learned

"The most precious wealth is the wealth acquired by the ear Indeed, of all wealth that wealth is the crown." - Thirukural By Thiruvalluvar
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Didn't I leave you a set of date handling functions when I was at your site? AddMonths would be one of them. The offset can be positive or negative.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
mickboland
Participant
Posts: 27
Joined: Sun Mar 20, 2005 4:23 am
Location: Brisbane, Australia

Post by mickboland »

Ray,

No date functions!!
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

tcj wrote:Don't forget to check for leap year :D
I would have done that in a premium post :) My favorite was is to do an ICONV on that output date and let the routine "clean it up" and tell me so with the return status code.
Post Reply