How do you find results that occurred in the past week?

Huuuze picture Huuuze · Jan 4, 2012 · Viewed 41.4k times · Source

I have a books table with a returned_date column. I'd like to see the results for all of the books with a returned date that occurred in the past week.

Any thoughts? I tried doing some date math, but Postgres wasn't happy with my attempt.

Answer

Eric picture Eric · Jan 4, 2012

You want to use interval and current_date:

select * from books where returned_date > current_date - interval '7 days'

This would return data from the past week including today.

Here's more on working with dates in Postgres.