Re: grabbing date of last Sunday?

From: Michael Glaesemann <grzm(at)seespotcode(dot)net>
To: rod(at)iol(dot)ie
Cc: blackwater dev <blackwaterdev(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: grabbing date of last Sunday?
Date: 2008-10-10 15:59:58
Message-ID: 42EC2788-A0F9-4A8F-AACD-0814053D1A33@seespotcode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Oct 10, 2008, at 11:36 , Raymond O'Donnell wrote:

> On 10/10/2008 16:29, blackwater dev wrote:
>> How can I grab the date from the last Sunday based on when I run
>> the query?
>
> select
> current_date
> - (extract(dow from current_date) || ' days')::interval;

Concatenations in math always make me shudder (and the above will give
you a timestamp besides):

SELECT CURRENT_DATE,
CURRENT_DATE - CAST(EXTRACT(DOW FROM CURRENT_DATE) as int) AS
date_integer_arithmetic,
CAST(CURRENT_DATE - (EXTRACT(DOW FROM CURRENT_DATE) * INTERVAL
'1 DAY') AS DATE) AS date_interval_arithmetic,
CAST(date_trunc('week', CURRENT_DATE) AS DATE) - 1 AS
non_standard;
date | date_integer_arithmetic | date_interval_arithmetic |
non_standard
------------+-------------------------+--------------------------
+--------------
2008-10-10 | 2008-10-05 | 2008-10-05 |
2008-10-05
(1 row)

Michael Glaesemann
grzm seespotcode net

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Pascal Cohen 2008-10-10 16:03:51 Re: Improve dump and restore time
Previous Message Raymond O'Donnell 2008-10-10 15:36:29 Re: grabbing date of last Sunday?