Hi- To find the no of mondays in a given month

A forum for discussing DataStage<sup>®</sup> basics. If you're not sure where your question goes, start here.

Moderators: chulett, rschirm, roy

Post Reply
swerajan
Participant
Posts: 42
Joined: Tue Aug 05, 2008 4:34 am

Hi- To find the no of mondays in a given month

Post by swerajan »

Hi How can we find the no of months in a given month in datastage parallel job. The date is not hardcoded,
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Probably the easiest is to create a routine the tests the first day of the month and the last day of the month to determine what day of the week each is, and to calculate the number of Mondays from that.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
swerajan
Participant
Posts: 42
Joined: Tue Aug 05, 2008 4:34 am

Post by swerajan »

Will i not be able to do it in a transformer... ??
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Probably. Use stage variables to give effect to the algorithm I described. A Routine will be re-usable.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
swerajan
Participant
Posts: 42
Joined: Tue Aug 05, 2008 4:34 am

Post by swerajan »

ok... but routines are written n basic lang and am new to them.can u suggest me any forums to learn the basic terms in them..also explain a bit more algorithm of yours.
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Routines for parallel jobs are not written in DataStage BASIC; they are written in C++.

Algorithm Design
1. Determine weekday of first day of month.
2. Determine weekday of last day of month.
3. Determine number of days in month.
4. If number of days in month = 28 then Number of Mondays = 4.
5. If number of days in month = 29 then If first of month is Monday then Number of Mondays = 5 else Number of Mondays = 4.
... and so on.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
BIuser
Premium Member
Premium Member
Posts: 238
Joined: Thu Feb 02, 2006 4:03 am
Location: South Africa

Post by BIuser »

You can call the follwon command line to do it. A bit crude, but it works:

cal jan 2008 |cut -c 4-5 |grep "[0-9]" |wc -l

*Note, cal jan 08 will give you Janury 8 AD, not 2008 :)
-------------------------
https://www.ssa.co.za
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

BIUser - that wasn't a crude answer at all - very elegant!!!!!
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

:D Very nice. For HP-UX (and perhaps others) it would need to be:

Code: Select all

cal 1 2008 |cut -c 4-5 |grep "[0-9]" |wc -l 
Now just run a year or more's worth into a hashed file and you'd be all set.
-craig

"You can never have too many knives" -- Logan Nine Fingers
umamahes
Premium Member
Premium Member
Posts: 110
Joined: Tue Jul 04, 2006 9:08 pm

Post by umamahes »

The below command gives only 3 mondays in january but actually we have 4 mondays.
cal 1 2008 |cut -c 4-5 |grep "[0-9]" |wc -l

You need to change the above command like below


cal 1 2008 |cut -c 4-6 |grep "[0-9]" |wc -l

cal 1 2008 |cut -c 4-6 |grep "[0-9]" |wc -l
4

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

Post by chulett »

Worked fine for me, what O/S are you running?
-craig

"You can never have too many knives" -- Logan Nine Fingers
BIuser
Premium Member
Premium Member
Posts: 238
Joined: Thu Feb 02, 2006 4:03 am
Location: South Africa

Post by BIuser »

To get around weirdness with space formatting, you can always use:

cal 1 2008 |cut -d " " -f 2| grep "[0-9]" |wc -l
-------------------------
https://www.ssa.co.za
BIuser
Premium Member
Premium Member
Posts: 238
Joined: Thu Feb 02, 2006 4:03 am
Location: South Africa

Post by BIuser »

chulett wrote: Now just run a year or more's worth into a hashed file and you'd be all set.
No need for a lot of effort

Code: Select all

for years in 2008 2009 2010; do for months in `seq 1 12`; do echo $years-$months| tr "\n" "|" ; cal $months $years |cut -d " " -f 2| grep "[0-9]" |wc -l   ; done; done > dates.txt
Then just import dates.txt into a hashfile :D
-------------------------
https://www.ssa.co.za
swerajan
Participant
Posts: 42
Joined: Tue Aug 05, 2008 4:34 am

Post by swerajan »

Hi all

thanks for all ur valuable answers. my req is bit diff,,, i need to find the no of mondays for a given month and the month value cums from a seq file say (20080701) 07 here represents the month of july,.. from here i hav to find the no of mondays for the month of july...
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

That is not your requirement, because the number of Mondays in July can vary over the years: it may be four, it may be five. Your actual requirement is to determine the number of Mondays in July of the year mentioned in the date. The solutions above can yield this.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Post Reply