Python241820
Python241820

Reputation: 1527

How to select specific dates in PostgreSQL?

My table:

create table example
(
        code           varchar(7),
        date           date,
CONSTRAINT pk_date PRIMARY KEY (code)
);

Dates:

insert into example(code, date) 
values('001','2016/05/12');
insert into example(code, date) 
values('002','2016/04/11');
insert into example(code, date) 
values('003','2017/02/03');

My problem: how to select the previous dates to six month from today ?

In MySQL I can use PERIOD_DIFF,but, in PostgreSQL?

Upvotes: 1

Views: 253

Answers (1)

Connected Wanderer
Connected Wanderer

Reputation: 313

You can try INTERVAL instruction :

SELECT date
FROM example
WHERE date < CURRENT_DATE + INTERVAL '6 months'
AND date > CURRENT_DATE;

You will get the dates from today to six months.

Upvotes: 2

Related Questions