Re: grabbing date of last Sunday? - Mailing list pgsql-general

From Michael Glaesemann
Subject Re: grabbing date of last Sunday?
Date
Msg-id 42EC2788-A0F9-4A8F-AACD-0814053D1A33@seespotcode.net
Whole thread Raw
In response to Re: grabbing date of last Sunday?  (Raymond O'Donnell <rod@iol.ie>)
List 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




pgsql-general by date:

Previous
From: Guillaume Lelarge
Date:
Subject: Re: Improve dump and restore time
Next
From: "Matthew T. O'Connor"
Date:
Subject: Re: Opteron vs. Xeon performance differences