Given this schema in a Postgres database:
CREATE TABLE person (
id serial PRIMARY KEY,
name text,
birth_date date,
);
How would I query the table to get the date of each person's next birthday after today?
For example if Bob's birth_date
is 2000-06-01 then his next birthday would be 2016-06-01.
Note: I'm not looking for the birth_date
+ a pre-defined interval
but rather the next anniversary of a person's birth.
I've written the equivalent in Python:
def next_birthday(self):
today = datetime.date.today()
next_birthday = self.birth_date.replace(year=today.year)
if next_birthday < today:
next_birthday = next_birthday.replace(year=today.year + 1)
return next_birthday
However I'd like to see if Postgres can do this in a more performant way.