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 dayI 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?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.
What is the use of such a function? If I saw this answer I would assume a bug somewhere (until reading the documentation).
GROUP BY DATEDIFF('month', "transaction_date", NOW())
And get a result set that shows the relative month in one column, and the sum in another.And those values relative values won't change every month like they would if I just grouped by
EXTRACT('year-month', "transaction_date")
(or whatever the syntax is for that)Also useful for a stored procedure or view than can then be JOINed in other queries as time goes on.
For example, find all users who have have been active at least 10 days. This provides a simple way to do a complex operation (if a user signs up Dec 29, a naive implementation won't catch that their 10 days is in January of the next year).
In my experience you almost always want to count intervals for analysis and not interval boundaries. For example when calculating someone’s age in years.
The use cases for interval boundaries seem to mostly be businesses rules based on contract dates.
Well, then you only need to compare the difference of the timestamp (or date) values with an interval of 10 days. e.g. end_time - start_time >= interval '10 days'
I'm surprised there is not native function. Does anyone know why?
Redshift, famously based off Postgres, chose to implement it