grabbing date of last Sunday?

Started by blackwater devover 17 years ago3 messagesgeneral
Jump to latest
#1blackwater dev
blackwaterdev@gmail.com

How can I grab the date from the last Sunday based on when I run the query?

For example I run it today, and I need to date of 10-5-08, if I ran it next
week, I would want 10-12-08, etc.

Thanks!

In reply to: blackwater dev (#1)
Re: grabbing date of last Sunday?

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;

:-)

Ray.

------------------------------------------------------------------
Raymond O'Donnell, Director of Music, Galway Cathedral, Ireland
rod@iol.ie
Galway Cathedral Recitals: http://www.galwaycathedral.org/recitals
------------------------------------------------------------------

#3Michael Glaesemann
grzm@seespotcode.net
In reply to: Raymond O'Donnell (#2)
Re: grabbing date of last Sunday?

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