Re: How to use daterange type?

From: Christophe Pettus <xof(at)thebuild(dot)com>
To: pgsql-general list <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to use daterange type?
Date: 2013-03-17 05:24:12
Message-ID: 976FC4C7-A6C1-4A5E-B4AF-26AEE0CC61D4@thebuild.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Mar 16, 2013, at 2:05 PM, Csanyi Pal wrote:

> So how can I use this table further eg. to get dates of the school days
> but without Saturdays and Sundays?

You can't do that directly (that kind of calendar operation is outside of the scope of a range type). You can, however, easily write selects that handle that:

postgres=# SELECT count(*)
postgres-# FROM generate_series(lower('[2012-09-01, 2012-12-24]'::daterange)::timestamp, upper('[2012-09-01, 2012-12-24]'::daterange)::timestamp, '1 day') as day
postgres-# WHERE EXTRACT(dow FROM day) BETWEEN 1 AND 5;
count
-------
82
(1 row)

In cases where you have more complex calendars (like lists of bank holidays), you could join against a table of them, or use a function that determines whether or not a particular day is holiday or not.

--
-- Christophe Pettus
xof(at)thebuild(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2013-03-17 08:31:14 Re: High RAM usage on postgres
Previous Message Ian Lawrence Barwick 2013-03-17 05:08:32 Re: How to use daterange type?