Skip to content
Advertisement

How do I select birthdays in the next 30 Days

I have a table with birthdates and I want to select all the birthdays that will come in the next 30 days.

The situation is, that all the birthdays are written in the form off 1999-09-15 which means that even if I tried selecting the next 30 days, the birthdays wouldn’t show up because the year is 1999.

I am running Microsoft Server 2016.

SELECT * from dbo.EMPLOYEES
WHERE DATE <= DATEADD(day, +30,GETDATE()) 
and   DATE >= getdate()
order by "DATE"

Advertisement

Answer

To get the birthdate, we need to work only on the days and the months, not on the year. Thats why we cannot get Where date between 2 dates.

SELECT 
  dateofbirth_c AS BIRTHDAY
 ,FLOOR(DATEDIFF(dd,EMP.dateofbirth_c,GETDATE()) / 365.25) AS AGE_NOW
 ,FLOOR(DATEDIFF(dd,EMP.dateofbirth_c,GETDATE()+30) / 365.25) AS AGE_30_Days_FROM_NOW
FROM 
  Employees EMP
WHERE 1 = (FLOOR(DATEDIFF(dd,EMP.dateofbirth_c,GETDATE()+30) / 365.25))
          -
          (FLOOR(DATEDIFF(dd,EMP.dateofbirth_c,GETDATE()) / 365.25))
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement