Re: First Saturday and Last Saturday of a month - Mailing list pgsql-general

From Allan Engelhardt
Subject Re: First Saturday and Last Saturday of a month
Date
Msg-id 3B71BAB8.3C2F114E@cybaea.com
Whole thread Raw
In response to First Saturday and Last Saturday of a month  ("Brian C. Doyle" <bcdoyle@mindspring.com>)
Responses Re: Re: First Saturday and Last Saturday of a month  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
"Brian C. Doyle" wrote:

> Hello all,
>
> I have a Function that gives me the fsunday of a week Based on the date you
> give it the select looks like
> SELECT Date('now') - timespan( text( date_part( 'dow', Date('now') ) ) ||
> 'days' )
> and if you run that today you will get
>        ?column?
> ------------------------
>   2001-08-05 00:00:00-04
> (1 row)
>
> Which is what I want it to do ...
>
> Now I need to setup a function that will give me the first Saturday of the
> month and then one to get the Last Saturday of the month.

It's too late to hack code, but:

For the first Saturday problem assign

    select date_part('dow', 'yyyy-mm-01'::DATE);

to a variable x.  Here yyy and mm is the year and month you are interested in.  The day you want is 'yyyy-mm-01' +
[(6-x)days]. 

Getting all the conversions right is left as an exercise for the reader :-)


Last Saturday problem is similar to above except for some sign reversals, but more interesting as you need the number
ofdays in the month.  I think you'll have to calculate that (for February) the hard way :-P  You *do* know the
algorithmfor testing if year yyyy is a leap year, don't you?   ((yyyy%4) && !(yyyy%400)) 


Allan.



pgsql-general by date:

Previous
From: missive@frontiernet.net (Lee Harr)
Date:
Subject: ERROR: AlterTableAddConstraint:
Next
From: Stephan Szabo
Date:
Subject: Re: ERROR: AlterTableAddConstraint: