query for records based on date
query for records based on date
От:
"Brandon Metcalf" <bmetcalf@nortel.com>
Дата:
If I have a table with an attribute of type "timestamp without time zone", what is the best way to query that table for records that are older than, say, one week. I suppose I could get the current time in epoch seconds and the time of the attribute in the same, and select those records where the difference is greater than the number of seconds in a week. Is there something easier like SELECT * FROM table WHERE attribute IS OLDER THAN one week; Thanks. -- Brandon
Re: query for records based on date
От:
Martín Marqués <martin@bugs.unl.edu.ar>
Дата:
El Lun 21 Feb 2005 17:00, Brandon Metcalf escribió: > If I have a table with an attribute of type "timestamp without time > zone", what is the best way to query that table for records that are > older than, say, one week. > > I suppose I could get the current time in epoch seconds and the time of > the attribute in the same, and select those records where the difference > is greater than the number of seconds in a week. Is there something > easier like > > SELECT * FROM table WHERE attribute IS OLDER THAN one week; SELECT * FROM table WHERE attribute < (now()::DATE - 7)::TIMESTAMP; -- 17:41:37 up 162 days, 7:36, 2 users, load average: 0.11, 0.12, 0.08 ----------------------------------------------------------------- Martín Marqués | select 'mmarques' || '@' || 'unl.edu.ar' Centro de Telematica | DBA, Programador, Administrador Universidad Nacional del Litoral -----------------------------------------------------------------
Re: query for records based on date
От:
"Brandon Metcalf" <bmetcalf@nortel.com>
Дата:
m == martin@bugs.unl.edu.ar writes: m> El Lun 21 Feb 2005 17:00, Brandon Metcalf escribi�:m> > If I have a table with an attribute of type "timestamp without timem> > zone", what is the best way to query that table for records that arem> > older than, say, one week.m> >m> > I suppose I could get the current time in epoch seconds and the time ofm> > the attribute in the same, and select those records where the differencem> > is greater than the number of seconds in a week. Is there somethingm> > easier likem> >m> > SELECT * FROM table WHERE attribute IS OLDER THAN one week; m> SELECT * FROM table WHERE attribute < (now()::DATE - 7)::TIMESTAMP; Thanks. -- Brandon
Re: query for records based on date
От:
Richard Huxton <dev@archonet.com>
Дата:
Brandon Metcalf wrote: > If I have a table with an attribute of type "timestamp without time > zone", what is the best way to query that table for records that are > older than, say, one week. > > I suppose I could get the current time in epoch seconds and the time of > the attribute in the same, and select those records where the difference > is greater than the number of seconds in a week. Is there something > easier like > > SELECT * FROM table WHERE attribute IS OLDER THAN one week; Look up "intervals" in the manuals - you can just subtract timestamps/dates. Probably also worth searching the mailing lists for "ago()" and "volatile" to learn about the issues with indexing. -- Richard Huxton Archonet Ltd