Reputation: 141
I have the following table in PostgreSQL:
table1
date_field_1 DATE
date_field_2 DATE
I would like to do the following calculation based on the date fields:
select
extract(YEAR from table1.date_field_1 - table1.date_field_2)*12 AS diff_1
, extract(MONTH from CURRENT_DATE - table1.date_field_2) AS diff_2
from table1
How can I do that?
Upvotes: 1
Views: 1355
Reputation: 3880
You can try to use the AGE
function
SELECT AGE(date_field_1, date_field_2) FROM table1;
Upvotes: 0
Reputation: 46219
You can try to use DATE_PART function and Subtraction.
select
DATE_PART('year', end) - DATE_PART('year', start)
from table1
Upvotes: 1
Reputation: 37473
Try this:
For Year
date_part('year',age(table1.date_field_1 , table1.date_field_2))
For Month
date_part('month',age(table1.date_field_1 , table1.date_field_2))
Upvotes: 0