Re: Last day of month - Mailing list pgsql-sql

From Michael Chaney
Subject Re: Last day of month
Date
Msg-id 20040226163702.GC22092@michaelchaney.com
Whole thread Raw
In response to Re: Last day of month  ("Greg Sabino Mullane" <greg@turnstep.com>)
List pgsql-sql
On Thu, Feb 26, 2004 at 03:07:52AM -0000, Greg Sabino Mullane wrote:
> 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>  
>  
> > How to find the last sunday/mon..../sat of any given month.
>  
> There is probably a smoother way to do it, but here is a
> quick little function to do what you ask. Feed it a date
> and a number, where 0 is Sunday, 1 is Monday, etc.

select date_trunc('month', current_date + '1 month'::interval) - '1 day'::interval +       (((3 - 7 -
to_char(date_trunc('month',current_date + '1 month'::interval) -       '1 day'::interval,'D')::int) %7)||'
days')::interval;

The "3" is the day of week (1 = Sunday, 7 = Saturday).  This equation
will return the date of the last "x" of the current month.  Change
"current_date" to be whatever date you wish to find the last "x" of.

Michael
--
Michael Darrin Chaney
mdchaney@michaelchaney.com
http://www.michaelchaney.com/


pgsql-sql by date:

Previous
From: "Leo Leo"
Date:
Subject: a few Questions about quoted varaibles in psql
Next
From: "Kumar"
Date:
Subject: Re: Return more than a record