SQL Select Upcoming Birthdays
I’m trying to write a stored procedure to select employees who have birthdays that are upcoming.
SELECT * FROM Employees WHERE Birthday > @Today AND Birthday < @Today + @NumDays
This will not work because the birth year is part of Birthday, so if my birthday was ’09-18-1983′ that will not fall between ’09-18-2008′ and ’09-25-2008′.
Is there a way to ignore the year portion of date fields and just compare month/days?
This will be run every monday morning to alert managers of birthdays upcoming, so it possibly will span new years.
Here is the working solution that I ended up creating, thanks Kogus.
SELECT * FROM Employees
WHERE Cast(DATEDIFF(dd, birthdt, getDate()) / 365.25 as int)
- Cast(DATEDIFF(dd, birthdt, futureDate) / 365.25 as int)
<> 0


arora on Mar 05, 2013
Note: I’ve edited this to fix what I believe was a significant bug. The currently posted version works for me.
This should work after you modify the field and table names to correspond to your database.
Basically, it gets the # of days from their birthday to now, and divides that by 365 (to avoid rounding issues that come up when you convert directly to years).
Then it gets the # of days from their birthday to a week from now, and divides that by 365 to get their age a week from now.
If their birthday is within a week, then the difference between those two values will be 1. So it returns all of those records.
joseph on Mar 05, 2013
In case someone is still looking for a solution in MySQL (slightly different commands), here’s the query:
xequnsruuh on Mar 05, 2013
Best use of datediff and dateadd. No rounding, no approximates, no 29th of february bug, nothing but date functions
dateOfBirth, GETDATE())
ageOfThePerson + 1, dateOfBirth)
GETDATE(), dateofNextBirthday)
Thanks to @Gustavo Cardoso, new definition for the age of the person
cheeseballlumpy82 on Mar 05, 2013
Sorry didn’t see the requirement to neutralize the year.
This should work.
pharme86 on Mar 05, 2013
I found the solution for this. This may save someone’s precious time.
Make a cross join with the dates and check for the comparison of month and dates.
johan-offer on Mar 05, 2013
This is solution for MS SQL Server:
It returns employees with birthdays in 30 days.
xequnsruuh on Mar 05, 2013
You could use DATE_FORMAT to extract the day and month parts of the birthday dates.
EDIT: sorry i didn’t see that he wasn’t using MySQL.
cheeseballlumpy82 on Mar 05, 2013
You could use the
DAYOFYEARfunction but be careful when you want to look for January birthdays in December. I think you’ll be fine as long as the date range you’re looking for doesn’t span the New Year.johan-offer on Mar 05, 2013
Assuming this is T-SQL, use DATEPART to compare the month and date separately.
http://msdn.microsoft.com/en-us/library/ms174420.aspx
Alternatively, subtract January 1st of the current year from everyone’s birthday, and then compare using the year 1900 (or whatever your epoch year is).
harry on Mar 05, 2013
Most of these solutions are close, but you have to remember a few extra scenarios. When working with birthdays and a sliding scale, you must be able to handle the transition into the next month.
For example Stephens example works great for birthdays up until the last 4 days of the month. Then you have a logic fault as the valid dates if today was the 29th would be :29, 30, AND then 1, 2, 3 of the NEXT month, so you have to condition for that as well.
An alternative would be to parse the date from the birthday field, and sub in the current year, then do a standard range comparison.
johan-offer on Mar 05, 2013
Another thought: Add their age in whole years to their birthday (or one more if their Birthday hasn’t happened yet and then compare as you do above. Use DATEPART and DATEADD to do this.
http://msdn.microsoft.com/en-us/library/ms186819.aspx
The edge case of a range spanning the year would have to have special code.
Bonus tip: consider using BETWEEN…AND instead of repeating the Birthday operand.
rajesh on Mar 05, 2013
This should work…
arora on Mar 05, 2013
I faced the same problem with my college project a few years ago. I responded (in a rather weasel way) by splitting the year and the date(MM:DD) in two separate columns. And before that, my project mate was simply getting all the dates and programatically going through them. We changed that because it was too inefficient – not that my solution was any more elegant either. Also, its probably not possible to do in a database that has been in use for a while by multiple apps.
joseph on Mar 05, 2013
No real answer, just another bordercase to consider..: People whose birthday is Feb 29th..
harry on Mar 05, 2013
Give this a try:
arora on Mar 05, 2013
Nuts! A good solution between when I started thinking about this and when I came back to answer.
I came up with:
You don’t need to cast getdate() as a datetime, right?
xequnsruuh on Mar 05, 2013
I hope this helps u in some way….
select Employeename,DOB from Employeemaster where day(Dob)>day(getdate()) and month(DOB)>=month(getDate())
johan-offer on Mar 05, 2013
This is a combination of a couple of the answers that was tested. This will find the next brithday after a certain date and the age they will be. Also the numdays will limit the range you are looking 7 days = week etc.
pharme86 on Mar 05, 2013
The best way to achieve the same is
fernando-correia on Mar 05, 2013
Upcoming Birthday for the Employee – Sqlserver
cheeseballlumpy82 on Mar 05, 2013
I used this for MySQL, probably not the most efficient way to query but simple enough to implement.
scott on Mar 05, 2013
i believe this ticket has been closed ages ago but for the benefit of getting the correct sql query please have a look.
fernando-correia on Mar 05, 2013
Liked the approach of @strelc, but his sql was a bit off. Here’s an updated version that works well and is simple to use:
bhtpjogetu on Mar 05, 2013
@p4bl0
He is using Microsoft SQL, not MySQL
rajesh on Mar 05, 2013
Try my solution… I have Informix database…
cheeseballlumpy82 on Mar 05, 2013
Better, Add the difference in years to the BIRTHDAY date, to make everything this year, and then do your compares