Re: birthday calculation

Поиск
Список
Период
Сортировка
От nolan@celery.tssi.com
Тема Re: birthday calculation
Дата
Msg-id 20030723070825.22354.qmail@celery.tssi.com
обсуждение исходный текст
Ответ на birthday calculation  (Ben <bench@silentmedia.com>)
Список 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 по дате отправления:

Предыдущее
От: Jean-Christian Imbeault
Дата:
Сообщение: Performance hit of foreign key constraints?
Следующее
От: Jean-Christian Imbeault
Дата:
Сообщение: \df PATTERN, what pattern?