Re:PostgreSQL's Missing DateDiff Function
by tomrod on Tuesday December 21, 2021 @07:01PM
(#29643695)
That doesn't make sense to me. Why wouldn't datediff be 0 in this case, as it is less than 365 days? Else one should simply:
and call it a day?
[ Parent
| on HN ]
Re:PostgreSQL's Missing DateDiff Function
by btilly on Tuesday December 21, 2021 @08:22PM
(#29644416)
Try it again, except looking for months. You'll get -11 instead of 1.
Re:PostgreSQL's Missing DateDiff Function
by castorp on Wednesday December 22, 2021 @08:16AM
(#29648878)
select extract(month from justify_interval(timestamp '2022-01-01' - timestamp '2021-11-01'))
returns 2
Re:PostgreSQL's Missing DateDiff Function
by saltcured on Wednesday December 22, 2021 @02:32PM
(#29653933)
I'm pretty sure this will provide the desired semantics, though I am away from a usable postgres and not bothering to test it.
CREATE 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;