pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Herouth Maoz <herouth@oumail.openu.ac.il>
To: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] Query based on date/time field
Date: Wed, 12 Aug 1998 15:25:39 +0300
Message-ID: <l03110703b1f737b60584@[147.233.159.109]> (raw)
In-Reply-To: <Pine.HPP.3.95.980812134339.4410A-100000@hp00076.ina.de>
References: <000601bdbfb1$80503a60$1304e426@loopy.sitestar.com>

At 15:50 +0300 on 12/8/98, Holger Mitterwald wrote:


> On Tue, 4 Aug 1998, William McCracken wrote:
>
> > I am new to SQL and was wondering how I would go
> > about selecting records from my database based on
> > the age of a date/time stamp.
> >
> > For example if I wanted to select records older than
> > 12 hours or 1 day.
> >
> > Any help would be appreciated.
>
> Sorry for my late response, but I was on holiday.
> I use some statement like this to select entries that are not older than 2
> weeks:
>
> SELECT DISTINCT landkreis FROM kneipe WHERE 1209600 >= (date_part('epoch',
> current_date) - date_part('epoch',datum));
>
> datum is of type datetime.
>
> what I do is the following: epoch returns the seconds since january 1st
> 1970 (I think). I substract the timestamp of each record from the current
> timestamp (current_date). The result has to be smaller than the duration
> for 2 weeks (in seconds, which is 1209600s).
>
> I dont know if there is a better solution, but it works fine.

I think the following is simpler and more intuitive:

SELECT * FROM the_table
WHERE the_date >= ('now'::datetime - '12 hours'::timespan);

This assumes that the field "the_date" in "the_table" is of type datetime.
If it isn't, just convert it by using datetime( the_date ).

The amount of time described by the type timespan is very intuitive - it
accepts, as you see, things like '12 hours', '1 day', or '2 months'. It
takes months correctly - with longer and shorter months taken into
accounts. More about it in the pgbuiltin manpage.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma





view thread (4+ messages)  latest in thread

Message-ID: <l03110703b1f737b60584@[147.233.159.109]>
Permalink:  ../l03110703b1f737b60584@%5B147.233.159.109%5D/
Also on:    postgresql.org/message-id/l03110703b1f737b60584@[147.233.159.109]

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: herouth@oumail.openu.ac.il
  Subject: Re: [SQL] Query based on date/time field
  In-Reply-To: <l03110703b1f737b60584@[147.233.159.109]>
  References: <000601bdbfb1$80503a60$1304e426@loopy.sitestar.com> <l03110703b1f737b60584@[147.233.159.109]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox