Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Birthdays in the next two weeks

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).

• 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.

## SearchDataManagement

• ### Who belongs on a high-performance data governance team?

Putting together a high-quality data governance team can be a challenge. Explore the necessary team members and best practices ...

• ### Soda launches cloud service to improve data observability

Data quality vendor Soda has had a busy 2021, building out new services and raising funding to help organizations identify and ...

• ### Bigeye raises \$17M Series A funding to boost data quality

Former Uber exec, CEO and co-founder of a startup, outlines the challenges and opportunities of enabling a new data trust ...

• ### Insightsoftware acquisition of Izenda targets embedded BI

ERP vendor Insightsoftware acquired embedded analytics specialist Izenda on April 15, marking its second purchase of an embedded ...

• ### Data scientist shortage leaves organizations uncertain

Despite their desire to use data science in their decision-making process, some organizations can't find qualified data ...

• ### Air Force's data overhaul makes analytics a priority

With its data locked in legacy systems that didn't communicate with one another, in 2017 the Air Force started to overhaul of its...

## SearchSAP

• ### S/4HANA Cloud SaaS ERP: Buying team overview

SAP's multi-tenant SaaS ERP, S/4HANA Cloud, is a viable choice for companies that need ease in their infrastructure management. ...

• ### SAP forms financial services partnership with Dediq

SAP and financial industry investment firm Dediq are forming a new business unit to develop applications that help banks and ...

• ### Unpatched applications threaten SAP security

Cyberattacks are a significant threat to unpatched, unprotected SAP applications, according to a new threat intelligence report ...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### Incorporate diversity and inclusion in technology design

DEI in technology is about more than creating a diverse workplace. We talked to a few DEI professionals about how teams build ...

• ### Microsoft previews OpenJDK distro to the delight of devs

In a move meant to attract more Java developers to its Azure cloud and further support the Java community, Microsoft launched a ...

• ### Supreme Court ruling on Java APIs eases developer worries

Now that the Supreme Court has ruled for Google over Oracle in their high-stakes copyright battle over Java APIs, developers can ...

## SearchDataCenter

• ### Programmable processor technology for next-gen data centers

The right processing technology can benefit your data center. Learn about advancements in CPU technologies, recent vendor ...

• ### Data processing units accelerate infrastructure performance

DPUs often run on networking packets to move information in the data center, instead of supporting processing workflows. Get an ...

• ### New Intel Ice Lake processors boost performance, security

Intel launches third-generation Xeon Scalable processors that bolster security, accelerate common data center workloads by 46% on...

## SearchContentManagement

• ### OpenText releases Cloud Editions content services updates

OpenText CE 21.2 includes federated document compliance that extends to Microsoft Office 365, along with a revamped content ...

• ### Adobe courts small businesses with Acrobat Pro package

As the pandemic disrupts paper workflows, Adobe courts small business users with simple webforms, digital signatures and payments...

• ### Know when to use a headless CMS and when it's not worth it

Headless CMSes aren't a silver bullet for every circumstance. Evaluate three solid use cases for a headless CMS and three ...

## SearchHRSoftware

• ### DEI tools arrive at critical time for HR

Employers are being held accountable for meeting diversity, equity and inclusion goals, especially executives. HR vendors are ...

• ### Oracle Cloud HCM introduces low-code/no-code template tools

Oracle Cloud HCM has been upgraded to ease development of HR processes and improve employee experience, the firm said.

• ### Hybrid workplace may give employers hiring edge

The post-pandemic office is leaning strongly in the direction of hybrid workplaces. Job candidates view working from home as a ...

Close