Re: birthday calculation - Mailing list pgsql-general

From nolan@celery.tssi.com
Subject Re: birthday calculation
Date
Msg-id 20030723070825.22354.qmail@celery.tssi.com
Whole thread Raw
In response to birthday calculation  (Ben <bench@silentmedia.com>)
List 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';




pgsql-general by date:

Previous
From: Jean-Christian Imbeault
Date:
Subject: Performance hit of foreign key constraints?
Next
From: Jean-Christian Imbeault
Date:
Subject: \df PATTERN, what pattern?