Re: Date for a week day of a month

From: "hubert depesz lubaczewski" <depesz(at)gmail(dot)com>
To: emilu(at)encs(dot)concordia(dot)ca
Cc: pgsql-sql-owner(at)postgresql(dot)org, "pgsql general list" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Date for a week day of a month
Date: 2007-07-04 08:58:01
Message-ID: 9e4684ce0707040158w3573972ev412263bb11ebe3d4@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 7/3/07, Emi Lu <emilu(at)encs(dot)concordia(dot)ca> wrote:
>
> Can I know how to get the date of each month's last Thursday please?
> Query: select getDateBaseOnWeekday('2007-04-01', 'Last Thursday');
> Result: 2007-04-26
>

you can easily do it without functions.
for example, this select:
SELECT
cast(d.date + i * '1 day'::interval as date)
FROM
(select '2007-04-01'::date as date) d,
generate_series(0, 30) i
WHERE
to_char(d.date, 'MM') = to_char( cast(d.date + i * '1 day'::interval as
date), 'MM')
AND to_char(cast(d.date + i * '1 day'::interval as date), 'D') = '5'
ORDER BY 1 DESC
LIMIT 1
;
does what you need.
to get last-thursday for another month, just change: (select
'2007-04-01'::date as date) d, to be 1st of any other month.

depesz

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Joshua N Pritikin 2007-07-04 09:02:03 Re: tsearch2 questions
Previous Message hubert depesz lubaczewski 2007-07-04 08:40:11 Re: tsearch2 questions