James
James

Reputation: 97

Postgresql difference between two columns dates?

Username    DateStart  DateFinish
James       2017-07-01 2017-09-14
Luigi       2017-08-02 2017-09-18
Francesco   2017-09-03 2017-10-25  

How calculate with sql difference between two date columns in days?

Upvotes: 7

Views: 17376

Answers (3)

João Nunes
João Nunes

Reputation: 3761

Try this. It extracts the number of days between 2 dates and creates a column called "days":

select extract(day from DateFinish - DateStart) as days from MyTable;

Upvotes: 2

Ashkan Kazemi
Ashkan Kazemi

Reputation: 1097

If you want to see the difference in a number (10 instead of a date value that has 10 days in it), you can obtain it with:

select extract(day from "DateFinish" - "DateStart") 

Upvotes: 2

Rahul Tripathi
Rahul Tripathi

Reputation: 172588

You can simply subtract them like

select "DateFinish"::date - "DateStart"::date;

And if the dates column are of datatype date then you can simply do:

select "DateFinish" - "DateStart"

Upvotes: 6

Related Questions