I have a table with anniversary dates. I want a query that returns me rows of anniversaries coming up in the next 10 days. For instance:
birthdate
---------
1965-10-10
1982-05-25
SELECT birthdate FROM Anniversaries WHERE mystical_magical_mumbo_jumbo <= 10
+------------+
| birthdate |
+------------+
| 1982-05-25 |
+------------+
1 row in set (0.01 sec)
I’d like to keep the query in the form x <= 10, because I’ll use that number 10 in other parts of the query, and if I set it to a variable, I can change it once everywhere by changing the variable, and not have to re-write the query.
As others have stated, you need to ignore the year in your comparison. The DAYOFYEAR() function is one way to do that.
Here’s a quick solution off the top of my head. It will return all birthdays in the next 10 days, even if it’s late December and the birthday is next year.
It DOES NOT handle leap years properly, so it will be off by 1 day for early March birthdays if this year is a leap year and the person was not born in a leap year or vice-versa. Leap years will also cause early January birthdays to show up one day off in late December sometimes. If anyone wants to add the leap year correction, feel free 🙂