I'm answering the X of what I feel is an XY Problem here; the OP doesn't need logic to exclude people with a birthday of 29 February, they need a way to work out what their birthday is for a specific (this) year, regardless of if it's a leap year.
This can be quite simple; just add the number of years that have passed since their birthday. SQL Server will automatically handle leap years, so 2004-02-29
plus 19 years would result in 2023-02-28
.
So, if you want people who have a birthday in the next week:
WITH Birthdays AS(
SELECT BD.BirthDate,
DATEADD(YEAR, YEAR(GETDATE()) - YEAR(BD.BirthDate),BD.BirthDate) AS BirthDay --This year's birthday
FROM (VALUES(CONVERT(date,'19840405')),
(CONVERT(date,'19961031')),
(CONVERT(date,'20040229')))BD(BirthDate))
SELECT *
FROM Birthdays
WHERE BirthDay >= CONVERT(date,GETDATE())
AND BirthDay < CONVERT(date, DATEADD(DAY, 7, GETDATE()));
Note that different countries do have different rules on when someone ages. In the UK (which is where I live) they aren't actually classed as legally aging until 01 March, however, in other countries it's 28 February. If you wanted it to be 01 March, then you could use an additional CTE to check if the DAY
of the 2 dates is the same; if it isn't then add a day to the BirthDay
to make it 01 March (rather than 28 February):
WITH Birthdays AS(
SELECT BD.BirthDate,
DATEADD(YEAR, YEAR(GETDATE()) - YEAR(BD.BirthDate),BD.BirthDate) AS BirthDay --This year's birthday
FROM (VALUES(CONVERT(date,'19840405')),
(CONVERT(date,'19961031')),
(CONVERT(date,'20040229')))BD(BirthDate)),
AdjustedBirthdays AS(
SELECT BirthDate,
CASE DAY(Birthdate) WHEN DAY(Birthday) THEN Birthday ELSE DATEADD(DAY,1,Birthday) END AS BirthDay
FROM Birthdays)
SELECT *
FROM AdjustedBirthdays
WHERE BirthDay >= CONVERT(date,GETDATE())
AND BirthDay < CONVERT(date, DATEADD(DAY, 7, GETDATE()));