Skip site navigation (1) Skip section navigation (2)

Re: Select data for current week only

From: Gerhard Heift <ml-postgresql-20081012-3518(at)gheift(dot)de>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Select data for current week only
Date: 2009-08-27 18:52:50
Message-ID: 20090827185250.GA5868@gheift.kawo1.rwth-aachen.de (view raw or flat)
Thread:
Lists: pgsql-general
On Thu, Aug 27, 2009 at 11:09:36AM -0700, BlackMage wrote:
> 
> Hey all,
> 
> I've been trying to figure this problem out with just using sql but I'm not
> sure how too. I have a table that holds events for each week but I only want
> to select events happening for the current week(Mon-Sun). So can anyone help
> me out with this just using sql? I've accomplished it sorta using php but
> only within the current 7 day range(example Thursday-Thursday) but not by
> the week.
> 
> The field I am using for sorting is a Date type with the format yyyy-mm-dd
> hh:mm:ss .

If you use a timestamp and an index on it, the best thing would be
something like this:

SELECT * FROM event where date_trunc('week', now()) <= event_date AND
event_date < date_trunc('week', now()) + '1 week'::interval

Regards,
  Gerhard

In response to

pgsql-general by date

Next:From: Sam MasonDate: 2009-08-27 19:06:41
Subject: Re: Select data for current week only
Previous:From: Edwin PlauchuDate: 2009-08-27 18:39:39
Subject: Re: Select data for current week only

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group