Getting all dates from previous 1 month, or 2 months in PostgreSQL

postgresql, postgresql-9.1, reporting-services

Solution

You likely want something like:

SELECT *
FROM the_table
WHERE date_column BETWEEN '2013-01-01' AND '2013-01-01' + INTERVAL '1' MONTH;

The `BETWEEN` operator is left-and-right inclusive. If you want right-exclusive you have to use separate `>=` and `<` tests instead.

Except for the `date_trunc` function that's ANSI-standard SQL, by the way, it's just that Microsoft SQL Server doesn't implement the ANSI interval types or ANSI date maths. Doing it a strictly ANSI-standard way would require you to replace the PostgreSQL-specific `date_trunc` with interval maths, like this example of how to get the months I just cooked up in Oracle. It's based on a subset of the sample data because of limitations in SQLFiddle for Oracle.

If you want to get the start of the current month, use `date_trunc`, eg:

SELECT date_trunc('2013-01-12');

will return `2013-01-01`. This can be combined with `INTERVAL` computations and the `extract` operation to do pretty much anything you need to with dates and times.

In this case, for the month before last I'd write:

SELECT *
FROM the_table
WHERE date_field BETWEEN date_trunc('month',current_date) - INTERVAL '2' MONTH 
                     AND (date_trunc('month',current_date) - INTERVAL '1' MONTH) - INTERVAL  '1' DAY;

(You want `INTERVAL '1' SECOND` instead of `INTERVAL '1' DAY` if you're working with timestamps not dates).

You'll notice the explicit parens for the interval subtraction. That's quite important, because dates are horrible nasty things. In date computations, `(a + b) + c` isn't necessarily the same as `a + (b + c)`. I was bitten by this recently, it's nasty.

Alternate phrasing, probably cleaner if you want `a <= x < b`:

SELECT *
FROM the_table
WHERE date_field >= date_trunc('month',current_date) - INTERVAL '2' MONTH
  AND date_field < date_trunc('month',current_date) - INTERVAL '1' MONTH;

Problem

I'm trying to extract all the dates from the previous 1 or 2 months from the DB table (adempiere) field "dateinvoice" For example if currently its 6 Jan 2012 I would like to extract all the data from 1 Dec 2011 - 31 Dec 2011 (previous 1 month), or 1 Nov 2011 - 31 Dec 2011 (previous 2 months) I've seen some answers using DateAdd, I've recently learn that since I'm using PostgreSQL the DateAdd function doesn't work. I'm using PostgreSQL 9.1. UPDATE I've followed Craigs recommendation and use SQLFiddle, my first time using it, so excuse me if i make any booboos The link is here http://sqlfiddle.com/#!1/1d5d4/1 http://sqlfiddle.com/#!1/1d5d4/3 I'm still getting errors even after changing the month to months... Update Number 2 Im using Visual Studio to search all these queries. And Craigs recommendation does actually work. I've been getting the errors when im in the Query Designer, but when i Edit As Text. there seems to be no problem. I'm not sure whether this error arises because of the Query Designer. But the query by Craig does work.. i think ill just stick to the text rather than the GUI Visual Studio offers for this problem . Thanks!

Original source