https://www.postgresql.org/docs/9.3/functions-datetime.html#...
Just did a search and got it from here, which shows it being used on the difference between two dates: https://stackoverflow.com/questions/24929735/how-to-calculat...
https://www.postgresql.org/docs/9.3/functions-datetime.html#...
Just did a search and got it from here, which shows it being used on the difference between two dates: https://stackoverflow.com/questions/24929735/how-to-calculat...
> The first century starts at 0001-01-01 00:00:00 AD, although they did not know it at the time
> This definition applies to all Gregorian calendar countries. There is no century number 0, you go from -1 century to 1 century. If you disagree with this, please write your complaint to: Pope, Cathedral Saint-Peter of Roma, Vatican.
select extract(year from '2021-01-01'::timestamptz - '2020-12-31'::timestamptz); > select ('2021-01-01'::timestamptz - '2020-12-31'::timestamptz) as diff;
1 daySo this is correct for years, right?
> select floor(extract(epoch from date_trunc('year', '2021-01-01'::timestamptz) - date_trunc('year', '2020-12-31'::timestamptz)) / 3600 / 24 / 365) as diff;
1I don't think there's a good, mathematical expression for determining something as arbitrary as dates. If there was, I don't think MS would implement a special function like DateDiff for it.
> # SELECT date_part('year', '2021-01-01'::timestamptz) - date_part('year', '2020-12-31'::timestamptz) as years;
Or what am I missing?
[edit: formatting]
If you e.g. want to check if two timestamps are more than a year apart you just compare the difference to an interval of 1 year.
It gets even strange for e.g. select DATEDIFF(year, '2021-12-01', '2022-01-01') which still returns 1 even though that's a whole month. I don't see a use case for this kind of result
I don't understand the purpose of such a calculation. If you want to check if two dates (or timestamps) fall into the same year, you compare the year part of them. If you want to check how far they are apart, you compare the difference between the two to an interval.
Semantically the code has to diff days (for example) but be aware that you crossed a year boundary (or any other).
select date_part('year','2021-01-01'::date) - date_part('year','2020-12-31'::date) as yeardiff;
yeardiff
----------
1
(1 row) extract(year from d1) - extract(year from d2)
and call it a day? select extract(month from justify_interval(timestamp '2022-01-01' - timestamp '2021-11-01'))
returns 2CREATE FUNCTION date_diff(granularity text, t1 timestamptz, t2 timestamptz) RETURNS int8 AS $$ SELECT date_part(granularity, date_trunc(granularity, t2) - date_trunc(granularity, t1))::int8 $$ LANGUAGE SQL;
I'm not sure what it's used for though
For months and days it would be a bit bigger
teo=> select extract(minutes from interval '70 minutes');
date_part
-----------
10
(1 row)
For a sum this should return 70 (since there are obviously 70 minutes in total). Instead, it gets normalized to 1 hour and 10 minutes, and returns the minute component only.The only, and unsatisfactory, answer I've seen to get a timedelta in wholey one unit is to extract the epoch and divide by the number of seconds in that unit.