Re: Last day of month

From: Frank Bax <fbax(at)sympatico(dot)ca>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Last day of month
Date: 2004-02-26 14:45:05
Message-ID: 5.2.1.1.0.20040226093422.029e0560@pop6.sympatico.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

At 11:30 PM 2/25/04, Joe Conway wrote:

>Greg Sabino Mullane wrote:
>>>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.
>
>oops...forget my last reply...I was a bit too quick on the draw. Try this
>instead:
>
>regression=# select date_trunc('month', current_date + '1
>month'::interval) - '1 day'::interval;
> ?column?
>---------------------
> 2004-02-29 00:00:00
>(1 row)
>
>Joe

But the original request was for a specific day-of-week. So use Joe's
answer above to get last day of month, and use 'dow' to determine the
day-of-week of that day. Let's call that dow1. If the day-of-week being
asked for is dow2 then:
if dow1 < dow2
return (last-day-of-month - dow1 - 7 + dow2)
else
return (last-day-of-month - dow1 + dow2)
I'm no good at coding pgsql functions, so I'm not going to attempt proper
syntax.

Frank

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Edmund Bacon 2004-02-26 14:55:14 Re: Scalar in a range (but textual not numeric)
Previous Message Kumar 2004-02-26 13:51:37 Return more than a record