0

I was looking for the answer to this question, and I found here that DATEDIFF() function is a way to do it when you have the dates. But, what if you don't have specific dates?

What if you want to find the interval of time between the date in 2 columns (rental_date, return_date).

I tried to use DATEDIFF() function, but it looks like you need the actual date.

2 Answers2

1

You can use date_part for finding differences between two dates in the PostgreSQL.

The syntax is like as follow.

DATE_PART('day', enddate - startdate);

You can get years, months, weeks, hours, minutes seconds as well. You can check more about the usage of date different and date part function in PostgreSQL by clicking on this link.

Dhiral Kaniya
  • 1,941
  • 1
  • 19
  • 32
  • If `enddate` and `startdate` are `DATE` values, `enddate - startdate` is enough –  Nov 18 '18 at 08:46
1

if your two column data type is date then you could do subtraction

select '2018-12-10'::date - '2018-11-18'::date

so in your case it would be

with t1 as
(
select '2018-12-10'::date as rental_date, '2018-11-18'::date as return_date
)
select rental_date-return_date from t1
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63