sql server birthday query

Get the list of an employee who have their birthdays today

  -- Sample Query
Create table Employee (name varchar(100),Birthdate date)
insert into Employee values('Peter','17/mar/1986')
insert into Employee values('John','09/jan/1986')
insert into Employee values('stew','18/mar/1986')
insert into Employee values('kennedy','16/mar/1986')
Example:
SELECT   Name,Birthdate AS Birthday,DATEDIFF(dd,EMP.Birthdate,GETDATE()) Days
,FLOOR(DATEDIFF(dd,EMP.Birthdate,GETDATE()) / 365.25) AS AgeNow,
case when datediff(dd,getdate(),convert(date,format(Getdate(),'yyyy')+'/'+format(Birthdate,'MM')+'/'+format(Birthdate,'dd')))<0 then
365-datediff(dd,getdate(),convert(date,format(Getdate(),'yyyy')+'/'+format(Birthdate,'MM')+'/'+format(Birthdate,'dd')))*(-1) else 
datediff(dd,getdate(),convert(date,format(Getdate(),'yyyy')+'/'+format(Birthdate,'MM')+'/'+format(Birthdate,'dd'))) End DaysRemaining,
case when format(Birthdate,'dd')=format(getdate(),'dd') and format(Birthdate,'MM')=format(getdate(),'MM')
then 'Today BirthDay' else '' end Status
FROM Employee EMP
Result:

Post a Comment

0 Comments