I am making a data driven site and want an admin page to display a list of people whose birthdays are in the next 14 days based on data from a MySQL database. How would I devise the SQL query to take in the current date and show me the birthdays from the database who are on or 14 days after the current date?
Thanks for mentioning that it's MySQL. That makes a big difference, because date functions are where database vendors stray the furthest from the SQL standard. Sometimes that's a good thing, though, and I really like MySQL's diverse and flexible date functions.
The part about this problem that's the toughest to figure out is what to do about year boundaries. You get into difficulty if you start by reconstructing the person's birthday this year --
select DATE_FORMAT(CURRENT_DATE,'%Y') + DATE_FORMAT(birthdate,'-%m-%d')
What if today is December 24 and the birthday is January 5? That's certainly within 14 days, but suddenly the comparison is not so straight-forward -- maybe you have to use the person's birthday next year, not this year, and do you subtract it from CURRENT_DATE or subtract CURRENT_DATE from the birthday?
Here's a better approach. My age, considered as an integer, does not go up until my next birthday is reached. If I'm 39 today, and in two weeks I'm 40, then my birthday must have occurred somewhere within those 14 days. Never mind whether we crossed a year boundary. All we need is a convenient formula for age in years. The MySQL docs give a splendid example --
To determine how many years old each of your pets is, compute the difference in the year part of the current date and the birth date, then subtract one if the current date occurs earlier in the calendar year than the birth date. The following query shows, for each pet, the birth date, the current date, and the age in years.select ( YEAR(CURRENT_DATE) - YEAR(birth) ) - ( RIGHT(CURRENT_DATE,5) < RIGHT(birth,5) ) as age
Here, YEAR() pulls out the year part of a date and RIGHT() pulls off the rightmost five characters that represent the MM-DD (calendar year) part of the date. The part of the expression that compares the MM-DD values evaluates to 1 or 0, which adjusts the year difference down a year if CURRENT_DATE occurs earlier in the year than birth.
Now all we have to do is compare the person's age in 14 days with the age today and Bob's your uncle --
select lastname, birthdate from yourtable where ( YEAR(DATE_ADD(CURRENT_DATE, INTERVAL 14 DAYS)) - YEAR(birthdate) ) - ( RIGHT(DATE_ADD(CURRENT_DATE, INTERVAL 14 DAYS),5) < RIGHT(birthdate,5) ) > ( YEAR(CURRENT_DATE) - YEAR(birthdate) ) - ( RIGHT(CURRENT_DATE,5) < RIGHT(birthdate,5) )
This problem is also discussed on pages 74-76 of Joe Celko's SQL for Smarties (ISBN 1-55860-323-9).
For More Information
- The Best SQL Web Links: tips, tutorials, scripts, and more.
- Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.
Dig Deeper on Oracle and SQL
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.