I work with a Postgres database. This DB has a table with users, who have a birthdate (date field). Now I want to get all users who have their birthday in the upcoming week....
My first attempt: SELECT id FROM public.users WHERE id IN (lange reeks) AND birthdate > NOW() AND birthdate < NOW() + interval '1 week'
But this does not result, obviously because off the year. How can I work around this problem?
And does anyone know what happen to PG would go with the cases at 29-02 birthday?
I'm not overly confident in this, but it seems to work in my testing. The key here is the
OVERLAPS
operator, and some date arithmetic.I assume you have a table:
Then I put some stuff into it:
This query will give me the people with birthdays in the next week:
The
date_trunc
truncates the date at the year, so it should get you up to the current year. I wound up having to add one year. This suggests to me I have an off-by-one in there for some reason. Perhaps I just need to find a way to get dates to round up. In any case, there are other ways to do this calculation.age
gives you the interval from the date or timestamp to today. I'm trying to add the years between the birthday and today to get a date in the current year.The real key is using
overlaps
to find records whose dates overlap. I use the anniversary date twice to get a point-in-time.I know this post is old, but I had the same issue and came up with this simple and elegant solution: It is pretty easy with age() and accounts for lap years... for the people who had their birthdays in the last 20 days:
We can use a postgres function to do this in a really nice way.
Assuming we have a table
people
, with a date of birth in the columndob
, which is a date, we can create a function that will allow us to index this column ignoring the year. (Thanks to Zoltán Böszörményi):Now, we need to query against the table, and the index. For instance, to get everyone who has a birthday in April of any year:
There is one gotcha: if our start/finish period crosses over a year boundary, we need to change the query:
To make sure we match leap-day birthdays, we need to know if we will 'move' them a day forward or backwards. In my case, it was simpler to just match on both days, so my general query looks like:
I have a django queryset method that makes this all much simpler:
Now, I can do things like:
Matching leap-day birthdays only on the day before, or only the day after is also possible: you just need to change the SQL test to a `>=' or '<=', and not adjust the start/finish in the python function.
In case you want it to work with leap years:
Then:
See also:
Getting all entries who's Birthday is today in PostgreSQL
Finally, to show the upcoming birthdays of the next 14 days I used this:
So: To solve the leap-year issue, I set both birthdate and current date to 2000, and handle intervals only from this initial correct dates.
To take care of the near end/beginning dates, I compared first the 2000 current date to the 2000 birthday interval, and in case current date is at the end of the year, and the birthday is at the beginning, I compared the 2001 birthday to the 2000 current date interval.
Exemple: birthdate between: jan 20 and feb 10
Why 1800? No matter may be any year;
In my registration form, I can inform the date of birth (with years) or just the birthday (without year), in which case I saved as 1800 to make it easier to work with the date