Skip to content

Instantly share code, notes, and snippets.

@johnparn
Created January 18, 2016 09:05
Show Gist options
  • Save johnparn/a624d7c51c691b99940b to your computer and use it in GitHub Desktop.
Save johnparn/a624d7c51c691b99940b to your computer and use it in GitHub Desktop.
-- Get age from birthdate
select (
case when
datepart(year,[BirthDate])>(1900)
AND datepart(year,[BirthDate])<datepart(year,getdate())
AND (datepart(month,[BirthDate])>=(1)
AND datepart(month,[BirthDate])<=(12))
AND (datepart(day,[BirthDate])>=(1)
AND datepart(day,[BirthDate])<=(31))
then
case isdate(CONVERT([datetime],[BirthDate],(0)))
when (1)
then floor(datediff(dayofyear,[birthdate],getdate())/(365.25))
when (0)
then NULL
end
end)
as age
from person
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment