Page 1 of 1

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

Posted: Fri Oct 31, 2008 7:50 am
by swerajan
Hi How can we find the no of months in a given month in datastage parallel job. The date is not hardcoded,

Posted: Fri Oct 31, 2008 8:53 am
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.

Posted: Fri Oct 31, 2008 9:16 am
by swerajan
Will i not be able to do it in a transformer... ??

Posted: Fri Oct 31, 2008 9:18 am
by ray.wurlod
Probably. Use stage variables to give effect to the algorithm I described. A Routine will be re-usable.

Posted: Fri Oct 31, 2008 9:30 am
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.

Posted: Fri Oct 31, 2008 10:52 am
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.

Posted: Sun Nov 02, 2008 11:58 am
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 :)

Posted: Sun Nov 02, 2008 12:37 pm
by ArndW
BIUser - that wasn't a crude answer at all - very elegant!!!!!

Posted: Sun Nov 02, 2008 1:02 pm
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.

Posted: Sun Nov 02, 2008 4:58 pm
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

Posted: Sun Nov 02, 2008 5:52 pm
by chulett
Worked fine for me, what O/S are you running?

Posted: Mon Nov 03, 2008 1:48 am
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

Posted: Mon Nov 03, 2008 2:59 am
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

Posted: Mon Nov 03, 2008 5:13 am
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...

Posted: Mon Nov 03, 2008 6:51 am
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.