Click here to Skip to main content
Click here to Skip to main content

SQL Server: SQL script to find upcoming birthdays

, 4 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
The script helps to find out upcoming birthdays even when days cross a year.


We can find upcoming birthdays by simply using between clause in normal days, but problem occurs when you reach the end of year. Given script will definitely help to solve this issue. 

Using the code

Create Temporary Calendar to store upcoming dates and join with Master Table on day and month.

The following script is quite lengthy, but works in all scenarios. We can use between clause to find out upcoming days, but query fails when current date reach to 29th Dec and we want to find birthday's coming in next 3 days. Given query will definitely show the birthday coming on 1st Jan in this scenario. 

--    Create Temporary Employee Master Table
    declare @TempEmp table (EmpName varchar(50), DOB datetime)
    insert into @TempEmp 
    select 'Sahana','1990-11-06' union
    select 'Princy','1989-08-25' union
    select 'Sameer','1984-07-06' union
    select 'Yogesh','1987-01-01' union
    select 'Sapna','1991-12-31'

--    Create Temporary Calendar Table To Store Upcoming Days
    declare @TempCal table (dates datetime)
    declare @CurrDate datetime
    declare @InNextDays int
    declare @BaseDate datetime

    set @CurrDate= getdate()    --convert(datetime,'29-dec-2013',103)    --
    set @InNextDays=3

--    Insert Upcoming Days In @TempCal
    declare @cnt int
    set @cnt=1
    while @cnt<=@InNextDays
    insert into @TempCal
    select dateadd(day, @cnt, @CurrDate)
    set @cnt=(@cnt+1)

--    Join On Day and Month
    select a.EmpName, a.DOB
    from @TempEmp a
    inner join @TempCal b on datepart(day, a.DOB)=datepart(day, b.Dates) and 
                datepart(month, a.DOB)=datepart(month, b.Dates)
    order by b.dates


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Team Leader Accord Fintech Pvt. Ltd.
India India
No Biography provided

Comments and Discussions

GeneralMy vote of 1 PinmemberHugo de Vreugd4-Apr-13 21:21 
GeneralRe: My vote of 1 Pinmemberprashant0556.s.more5-Apr-13 0:31 
GeneralRe: My vote of 1 PinmemberHugo de Vreugd5-Apr-13 2:21 
I hope the editor will keep my + signs this time Wink | ;) .
The correct and complete SQL should be:
select * from @TempEmp
where datediff(d, convert(varchar(4), year(@currdate)) + right(convert(varchar(10), dob, 112), 4), @currdate) between -3 and 0
or datediff(d, convert(varchar(4), year(@currdate) + 1) + right(convert(varchar(10), dob, 112), 4), @currdate) between -3 and 0

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 4 Apr 2013
Article Copyright 2013 by prashant0556.s.more
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid