Determine the month difference between 2 dates

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
rajeevs
Participant
Posts: 14
Joined: Thu Jan 12, 2006 10:42 am

Determine the month difference between 2 dates

Post by rajeevs »

Hello All:

I have to find the difference in month between 2 dates. I know we could do it in SQL, but unfortunately these dates are derived from other columns and hence would need to be done within DataStage. Any help would greatly be appreciated!!

For example:
Date1=2001-01-01
Date2=2004-01-01
Result should be : 36 (months)

Thanks All.
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

You can use DaysSinceFromDate() or convert the dates to Ordinal dates and subtract them. Both will give you number of days between the two dates. Then divide the number by 30.46 to get a close approximation.
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

An "all terms" search for month difference dates returned 17 hits. Somewhere in there lies an answer.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Madhusv
Participant
Posts: 35
Joined: Sat May 07, 2005 1:38 am
Contact:

Post by Madhusv »

You can get this by using Date functions and below logic appropriately

Stage1: IF Date1>Date2 Then A Else B
Stage2: IF A then ((GetYear(Date1)-GetYear(Date2))*12+GetMonth(Date1))-GetMonth(Date2) Else ((GetYear(Date2)-GetYear(Date1))*12+GetMonth(Date2))-GetMonth(Date1)

Thanks,
Madhu
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Nice logic. Would have been improved by use of the correct function names, such as MonthFromDate() and YearFromDate() in the Transformer stage. Also, ought you not to compare MonthDayFromDate() values to determine A or B approach, rather than the actual dates?
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Sainath.Srinivasan
Participant
Posts: 3337
Joined: Mon Jan 17, 2005 4:49 am
Location: United Kingdom

Post by Sainath.Srinivasan »

Just to add to the logic.....you need to check whether the 'dd' parts are different.

i.e. 2001-01-02 to 2001-02-01 is not one month (if my understanding of your requirement is correct)

So you can add
+ (if second_dd > first_dd then -1 else 0)
Post Reply