Get the first and last day of previous month

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
raji33
Premium Member
Premium Member
Posts: 151
Joined: Thu Sep 23, 2010 9:21 pm
Location: NJ

Get the first and last day of previous month

Post by raji33 »

Hi All,

I have requirement to get data between previous month first day and last day.

Below is the code i am using to get last day of previous month but not knowing how to get first day of last month. I have some info on Month.First(server edition) but i need in parallel . Any suggestions?

Code: Select all

DateFromDaysSince(-1, stringtodate(right(0:MonthFromDate(CurrentDate()),2):'-':'01':'-': YearFromDate(CurrentDate()),"%mm-%dd-%yyyy"))
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Getting the last day of a month is the tricky part, getting the first day should be simple since it is always '1' so hard-code it in your transformation much like you are already doing.
-craig

"You can never have too many knives" -- Logan Nine Fingers
raji33
Premium Member
Premium Member
Posts: 151
Joined: Thu Sep 23, 2010 9:21 pm
Location: NJ

Post by raji33 »

Thanks chulett . I am not clear on hard-code '1' , let me give an example of what i am looking for
ex:
if i run job on june 1, 2013

output:
2013-05-01
2013-05-02
2013-05-03
-
-
-
- so on
2013-05-31

and if the month has 30 days it should display 30 days data.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

I understood what you are looking for, I do believe, at least in the original post. I was responding to your specific statement that you are "not knowing how to get first day of last month". You were already getting the "last month" according to what you posted so was just trying to point out that figuring out what the first day of that was is pretty darn straight-forward.

So... what exact help are you looking for here? It looks like you've just expanded the scope of your question, hence my question.
-craig

"You can never have too many knives" -- Logan Nine Fingers
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

What Craig means is that you can hard code day number as 1 to get the first of any month. However, you can use date functions exclusively.

For example: to get the first day of the current month, subtract the current day number from the date:

Code: Select all

DateFromDaysSince(1 - MonthDayFromDate(CurrentDate()), CurrentDate())
Assuming we have that result in stage variable svCurrentMonthFirst, to get the first day of the previous month you could use :

Code: Select all

DateOffsetByComponents(svCurrentMonthFirst, 0, -1, 0)
And to get the last day of the previous month you could use the fact that it is one day prior to the first day of the current month:

Code: Select all

DateOffsetByComponents(svCurrentMonthFirst, 0, 0, -1)
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
jerome_rajan
Premium Member
Premium Member
Posts: 376
Joined: Sat Jan 07, 2012 12:25 pm
Location: Piscataway

Post by jerome_rajan »

ray.wurlod wrote:....

Code: Select all

DateOffsetByComponents(svCurrentMonthFirst, 0, -1, 0)
...
You've got to be really careful of using this. It won't always give you what you expect unless you set your expectations in line with how it really works :? .Just remembered this discussion viewtopic.php?t=149999
Jerome
Data Integration Consultant at AWS
Connect With Me On LinkedIn

Life is really simple, but we insist on making it complicated.
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

It WILL always give me what I expect, because I made sure that the date argument is the first of the month.
:)
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
jerome_rajan
Premium Member
Premium Member
Posts: 376
Joined: Sat Jan 07, 2012 12:25 pm
Location: Piscataway

Post by jerome_rajan »

I've been wary of this function ever since. Always try finding alternates, albeit longer and more cumbersome. There ought to be a more elaborate documentation replete with examples to explain the modus operandi of this function. :(
Jerome
Data Integration Consultant at AWS
Connect With Me On LinkedIn

Life is really simple, but we insist on making it complicated.
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

I think my research conclusions in the other thread suffice as documentation of the modus operandi, as you put it. This is How It Works, even though it's not what you want it to do.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
bhasds
Participant
Posts: 79
Joined: Thu May 27, 2010 1:49 am

Post by bhasds »

Hi Raji33,

You may try the below to get the first day of last month-

Code: Select all

CurrentDate()[1,4]:"-"If  CurrentDate()[6,2]-1 < 10 Then 0: CurrentDate()[6,2]-1 Else  CurrentDate()[6,2]-1):"-":"01
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

My method does not involve several data type conversions. Yours does. These all take machine cycles.

Also yours will fail if the current month is January.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
raji33
Premium Member
Premium Member
Posts: 151
Joined: Thu Sep 23, 2010 9:21 pm
Location: NJ

Post by raji33 »

Thanks Ray it worked.
Post Reply