From: | nolan(at)celery(dot)tssi(dot)com |
---|---|
To: | bench(at)silentmedia(dot)com (Ben) |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: birthday calculation |
Date: | 2003-07-23 07:08:25 |
Message-ID: | 20030723070825.22354.qmail@celery.tssi.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
> I get their age in days, which doesn't let me take leap years into
> account. Is there a simple magic date_diff function that I'm missing? Or
> lacking that some other way to get postgres to do the date calculations?
There are two ways to do this, the quick and dirty and almost accurate
method, which is to divide by 365.25 and truncate to years, or to
extract the year.
Here's a function I wrote that returns the age in years. I suppose
there are better ways to do this, when I wrote this several months
ago I knew less about date handling in postgres than I do now.
But it still works for me and I haven't had the inclination to rewrite it:
create or replace function public.age_in_years(date, date)
returns integer as
'
DECLARE
date1 alias for $1;
date2 alias for $2;
BEGIN
if date1 is null or date2 is null then
return NULL;
end if;
return cast( coalesce(substring(age(date1, date2) from ''(.*) year''),''0'')
as int);
END
' language 'plpgsql';
From | Date | Subject | |
---|---|---|---|
Next Message | Chris Travers | 2003-07-23 07:22:25 | Re: 0/1 vs true/false |
Previous Message | Jean-Christian Imbeault | 2003-07-23 07:05:00 | Performance hit of foreign key constraints? |