185
SELECT Table.date FROM Table WHERE date > current_date - 10;

Does this work on PostgreSQL?

8 Answers 8

298

Yes this does work in PostgreSQL (assuming the column "date" is of datatype date) Why don't you just try it?

The standard ANSI SQL format would be:

SELECT Table.date 
FROM Table 
WHERE date > current_date - interval '10' day;

I prefer that format as it makes things easier to read (but it is the same as current_date - 10).

9
  • 1
    The query should be: SELECT Table.date FROM Table WHERE date > current_date - interval '10 day'; Jun 6, 2016 at 15:30
  • 8
    @user2694306: interval '10 day' is the Postgres syntax. interval '10' day is the syntax based on the SQL standard and also supported by Postgres
    – user330315
    Jun 6, 2016 at 16:04
  • 1
    interval should be 9. 10 actually gives you 11 days back from today.
    – David He
    Aug 25, 2016 at 7:26
  • 1
    @DavidHe: this does the same thing as the original answer. Which uses 10, not 9
    – user330315
    Aug 25, 2016 at 7:34
  • 3
    Just a note: On Redshift, @user2694306's formulation works: interval '10 day'. interval '10' day doesn't work on Redshift.
    – Ben
    Jul 27, 2018 at 16:02
56

http://www.postgresql.org/docs/current/static/functions-datetime.html shows operators you can use for working with dates and times (and intervals).

So you want

SELECT "date"
FROM "Table"
WHERE "date" > (CURRENT_DATE - INTERVAL '10 days');

The operators/functions above are documented in detail:

14

My understanding from my testing (and the PostgreSQL dox) is that the quotes need to be done differently from the other answers, and should also include "day" like this:

SELECT Table.date
  FROM Table 
  WHERE date > current_date - interval '10 day';

Demonstrated here (you should be able to run this on any Postgres db):

SELECT DISTINCT current_date, 
                current_date - interval '10' day, 
                current_date - interval '10 days' 
  FROM pg_language;

Result:

2013-03-01  2013-03-01 00:00:00 2013-02-19 00:00:00
1
  • Actually, I missed noticing that bradley's answer got it right. Anyway, I'll leave mine here as proof that it's the correct way. The accepted answer is wrong (at least for the version of Postgre I'm running) Mar 1, 2013 at 0:56
10

The suggested answers already seem to solve the questions. But as an addition I am suggesting to use the NOW() function of PostgreSQL.

SELECT Table.date 
FROM Table 
WHERE date > now() - interval '10' day;

Additionally you can even specifiy the time zone which can be really handy.

NOW () AT TIME ZONE 'Europe/Paris'
9

Starting with Postgres 9.4 you can use the AGE function:

SELECT Table.date FROM Table WHERE AGE(Table.date) <= INTERVAL '10 day';

3

Just generalising the query if you want to work with any given date instead of current date:

SELECT Table.date
  FROM Table 
  WHERE Table.date > '2020-01-01'::date - interval '10 day'
1
  • this! thx u, this worked for me
    – ERJAN
    Oct 28, 2023 at 13:15
0

I would check datatypes.

current_date has "date" datatype, 10 is a number, and Table.date - you need to look at your table.

0

you can use between too:

SELECT Table.date
  FROM Table 
  WHERE date between current_date and current_date - interval '10 day';
1
  • 1
    The between has to be with the lowest value first, so this would be correctly done as SELECT Table.date FROM Table WHERE date between current_date - interval '10 day' and current_date;
    – type
    May 30, 2020 at 2:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.