Hey everyone. Here is my situation… I need to craft a sql query against a postgresql server that will return all records created within the past 5 minutes, rounded down to the lowest minute. So if cron kicks the query off at 12:05:25.000, it needs to query all records created since 12:00:00.000. So I guess I really have two issues.
Here is the current query:
select * from callhistory where created>date_trunc('minute', timestamp (current_timestamp-interval '5' minute));
It doesn’t return anything. Also, the format of the created field is “2011-05-18 18:11:32.024.”
Any help would be appreciated.
The syntax is your
date_truncis a bit off:You could also use
now()in place ofcurrent_timestamp:And an example:
UPDATE: Looks like PostgreSQL version 8 is a little stricter on the format for
interval, the fine manual says that you should useinterval '$n $unit'where$nis the number of things and$unitis the time unit. Version 9 allows you to sayinterval '$n' $unitwithout complaint but version 8 converts your interval to 0 if you don’t use the documented format. So, you should use this for both version 8 and version 9: