Increment Date Field by 6 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
Christina Lim
Participant
Posts: 74
Joined: Tue Sep 30, 2003 4:25 am
Location: Malaysia

Increment Date Field by 6 months

Post by Christina Lim »

hallo all,

I would like to manipulate a date field.

My date value is YYYYMMDD.
I would like to get the next 6 months from the date.

For example :
Original Date : 20030303
New Date : 20030903

Original Date : 20030703
New Date : 20040103

Am I supposed to convert to internal date for the manipulation?
Or are there any built-in transform or routines that I can use to achieve these?

Would so much appreciate your advice for the solution.

thanz and Regards,
Christina
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

You can add 183 days to the internal format, or use substrings to add six months to the date. The latter would need to be done in a routine.

Code: Select all

FUNCTION AddMonthsToDate(BaseDate, Months)
Equate RoutineName To "AddMonthsToDate"
Ans = @NULL
Test = Iconv(BaseDate, "DYMD")
If Status() = 0 And BaseDate Matches "8N" Then
   If Months Matches "1N0N" : @VM : "'+'1N0N"
   Then
      Year = BaseDate[1,4]
      Month = BaseDate[5,2]
      Day = BaseDate[7,2]
      Month += 6
      If Month > 12
      Then
         Month -= 12
         Year += 1
      End
      Ans = Fmt(Year,"R%4") : Fmt(Month("R%2") : Day
   End
   Else
      Call DSTransformError("Non-integer months in Arg2.", RoutineName)
   End
End
Else
   Call DSTransformError("Invalid date " : Quote(BaseDate), RoutineName)
End
RETURN(Ans)
I leave it as an exercise to add code to handle a negative second argument.
Last edited by ray.wurlod on Fri Jul 02, 2004 4:31 pm, edited 1 time in total.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Christina Lim
Participant
Posts: 74
Joined: Tue Sep 30, 2003 4:25 am
Location: Malaysia

Post by Christina Lim »

Hallo Ray,

Thanz for the routine. May I know what is the second argument for?

Regards,
Christina
smoffa
Premium Member
Premium Member
Posts: 40
Joined: Wed Jun 30, 2004 6:00 am
Location: Orlando, FL
Contact:

Post by smoffa »

I think Ray meant for the "Months" to be a variable to store the number of months to increment the current month. So, if I'm correct, you can replace the "6" with "Months".

A negative second argument is to subtract months from current month.

Also, don't you need a ":" after the @VM?
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Well spotted, I've edited the code. :oops:
Dots and colons are SO hard to see on a laptop monitor! :wink:
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Christina Lim
Participant
Posts: 74
Joined: Tue Sep 30, 2003 4:25 am
Location: Malaysia

Post by Christina Lim »

Thanz a lot....

I managed to put it the logic just fine.

Thanz again!!
Post Reply