Pages

Thursday, March 31, 2011

Calculating upcoming birthdays from date of birth

select * from Customers c
where dateadd(year, 1900-year(dob), dob)
between dateadd(year, 1900-year(getdate()), getdate())
and dateadd(year, 1900-year(getdate()), getdate())+7

The above Query Displays next 7 days up coming birthdays
——————————————————————-

Calculating Age of a Person

SELECT *
   FROM Employee
  WHERE DATEDIFF(dd,
                 DATEADD(yy, -(DATEPART(yy,GETDATE())-1900),GETDATE()), --Today
                 DATEADD(yy, -(DATEPART(yy,BirthDate)-1900),BirthDate)
                ) BETWEEN 1 AND 10

No comments:

Post a Comment