Re: Fwd: Date math

From: Justin <justin(at)emproshunts(dot)com>
To: Adam Rich <adam(dot)r(at)sbcglobal(dot)net>
Cc: Guy Flaherty <naoshika(at)gmail(dot)com>, Postgresql Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Fwd: Date math
Date: 2009-06-28 04:39:08
Message-ID: 4A46F3EC.50603@emproshunts.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Adam Rich wrote:
> Guy Flaherty wrote:
>>
>>
>> You could use the extract() function to calculate the day of year of
the person's birthdate and then check if this number is within today's
day of year and range of days you want to check for, for example,
today's day of year + 30 days to be within a month. That way you don't
need to worry about years at all. You may need to double check this will
work on the leap years though!
>>
>>
>
> Thanks! that's even better than what I just came up with:
>
> birth_date + ((interval '1 year') * ceil(EXTRACT(DAYS FROM (now() -
birth_date))/365.25))
>
> And I like the "Day of year" solution because (I think) I can use a
functional index on that value.

it kind of ugly looking but here is one that uses only math and no
problem with leap years or anything

select current_date, '07-02-1979'::date +
((date_part('year',current_date) - date_part( 'year',
'07-02-1979'::date))::text||'year')::interval

So the select statement might look like this

select birth_day
where birthday + ((date_part('year',current_date) - date_part(
'year',birth_day))::text||'year')::interval Between now() and now() +
'90 day'::interval

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Joe Conway 2009-06-28 04:52:30 Re: Date math
Previous Message Adam Rich 2009-06-28 04:27:33 Re: Fwd: Date math