# 如何根据出生date和getDate（）计算年龄（以年为单位）

``ID Name DOB 1 John 1992-01-09 00:00:00 2 Sally 1959-05-20 00:00:00` `

` `ID Name AGE DOB 1 John 17 1992-01-09 00:00:00 2 Sally 50 1959-05-20 00:00:00` `

` `DECLARE @dob datetime SET @dob='1992-01-09 00:00:00' SELECT DATEDIFF(hour,@dob,GETDATE())/8766.0 AS AgeYearsDecimal ,CONVERT(int,ROUND(DATEDIFF(hour,@dob,GETDATE())/8766.0,0)) AS AgeYearsIntRound ,DATEDIFF(hour,@dob,GETDATE())/8766 AS AgeYearsIntTrunc` `

OUTPUT：

` `AgeYearsDecimal AgeYearsIntRound AgeYearsIntTrunc --------------------------------------- ---------------- ---------------- 17.767054 18 17 (1 row(s) affected)` `

` `DECLARE @Now datetime, @Dob datetime SELECT @Now='1990-05-05', @Dob='1980-05-05' --results in 10 --SELECT @Now='1990-05-04', @Dob='1980-05-05' --results in 9 --SELECT @Now='1989-05-06', @Dob='1980-05-05' --results in 9 --SELECT @Now='1990-05-06', @Dob='1980-05-05' --results in 10 --SELECT @Now='1990-12-06', @Dob='1980-05-05' --results in 10 --SELECT @Now='1991-05-04', @Dob='1980-05-05' --results in 10 SELECT (CONVERT(int,CONVERT(char(8),@Now,112))-CONVERT(char(8),@Dob,112))/10000 AS AgeIntYears` `

` `DECLARE @Now datetime, @Dob datetime SELECT @Now='1990-05-05', @Dob='1980-05-05' --results in 10.000000000000 --SELECT @Now='1990-05-04', @Dob='1980-05-05' --results in 9.997260273973 --SELECT @Now='1989-05-06', @Dob='1980-05-05' --results in 9.002739726027 --SELECT @Now='1990-05-06', @Dob='1980-05-05' --results in 10.002739726027 --SELECT @Now='1990-12-06', @Dob='1980-05-05' --results in 10.589041095890 --SELECT @Now='1991-05-04', @Dob='1980-05-05' --results in 10.997260273973 SELECT 1.0* DateDiff(yy,@Dob,@Now) +CASE WHEN @Now >= DATEFROMPARTS(DATEPART(yyyy,@Now),DATEPART(m,@Dob),DATEPART(d,@Dob)) THEN --birthday has happened for the @now year, so add some portion onto the year difference ( 1.0 --force automatic conversions from int to decimal * DATEDIFF(day,DATEFROMPARTS(DATEPART(yyyy,@Now),DATEPART(m,@Dob),DATEPART(d,@Dob)),@Now) --number of days difference between the @Now year birthday and the @Now day / DATEDIFF(day,DATEFROMPARTS(DATEPART(yyyy,@Now),1,1),DATEFROMPARTS(DATEPART(yyyy,@Now)+1,1,1)) --number of days in the @Now year ) ELSE --birthday has not been reached for the last year, so remove some portion of the year difference -1 --remove this fractional difference onto the age * ( -1.0 --force automatic conversions from int to decimal * DATEDIFF(day,DATEFROMPARTS(DATEPART(yyyy,@Now),DATEPART(m,@Dob),DATEPART(d,@Dob)),@Now) --number of days difference between the @Now year birthday and the @Now day / DATEDIFF(day,DATEFROMPARTS(DATEPART(yyyy,@Now),1,1),DATEFROMPARTS(DATEPART(yyyy,@Now)+1,1,1)) --number of days in the @Now year ) END AS AgeYearsDecimal` `

（yyyyMMdd – yyyyMMdd）/ 10000 =全年的差额

` `declare @as_of datetime, @bday datetime; select @as_of = '2009/10/15', @bday = '1980/4/20' select Convert(Char(8),@as_of,112), Convert(Char(8),@bday,112), 0 + Convert(Char(8),@as_of,112) - Convert(Char(8),@bday,112), (0 + Convert(Char(8),@as_of,112) - Convert(Char(8),@bday,112)) / 10000` `

` `20091015 19800420 290595 29` `

` `SELECT FLOOR((CAST (GetDate() AS INTEGER) - CAST(Date_of_birth AS INTEGER)) / 365.25) AS Age` `

` `create function [dbo].[AgeAtDate]( @DOB datetime, @PassedDate datetime ) returns int with SCHEMABINDING as begin declare @iMonthDayDob int declare @iMonthDayPassedDate int select @iMonthDayDob = CAST(datepart (mm,@DOB) * 100 + datepart (dd,@DOB) AS int) select @iMonthDayPassedDate = CAST(datepart (mm,@PassedDate) * 100 + datepart (dd,@PassedDate) AS int) return DateDiff(yy,@DOB, @PassedDate) - CASE WHEN @iMonthDayDob <= @iMonthDayPassedDate THEN 0 ELSE 1 END End` `

` `SELECT CASE WHEN dateadd(year, datediff (year, DOB, getdate()), DOB) > getdate() THEN datediff(year, DOB, getdate()) - 1 ELSE datediff(year, DOB, getdate()) END as Age FROM <table>` `

` `DATEDIFF(yy, @date, GETDATE()) - CASE WHEN (MONTH(@date) > MONTH(GETDATE())) OR (MONTH(@date) = MONTH(GETDATE()) AND DAY(@date) > DAY(GETDATE())) THEN 1 ELSE 0 END` `

` `DATEDIFF(year, @date, GETDATE()) - CASE WHEN DATEPART(dayofyear, @date) > DATEPART(dayofyear, GETDATE()) THEN 1 ELSE 0 END` `

[注意：SQL中无处不在BOL / MSDN是`DATEPART(dayofyear, ...)`返回的实际logging！ 我理解它是1-366范围内的数字; 最重要的是，它不会按照`DATEPART(weekday, ...)``DATEPART(weekday, ...)``SET DATEFIRST`语言环境更改。]

` `SELECT DATEDIFF(YY, DateOfBirth, GETDATE()) - CASE WHEN RIGHT(CONVERT(VARCHAR(6), GETDATE(), 12), 4) >= RIGHT(CONVERT(VARCHAR(6), DateOfBirth, 12), 4) THEN 0 ELSE 1 END AS AGE` `

` `DECLARE @DOB datetime SET @DOB='19851125' SELECT Datepart(yy,convert(date,GETDATE())-@DOB)-1900` `

` `DECLARE @BirthDate DATE = '09/06/1979' SELECT ( YEAR(GETDATE()) - YEAR(@BirthDate) - CASE WHEN (MONTH(GETDATE()) * 100) + DATEPART(dd, GETDATE()) > (MONTH(@BirthDate) * 100) + DATEPART(dd, @BirthDate) THEN 1 ELSE 0 END )` `
` `DECLARE @DOB datetime set @DOB ='11/25/1985' select floor( ( cast(convert(varchar(8),getdate(),112) as int)- cast(convert(varchar(8),@DOB,112) as int) ) / 10000 )` `

` `SELECT FLOOR(DATEDIFF(DAY, @date1 , @date2) / 365.25)` `

` `SELECT ID, Name, DATEDIFF(yy,CONVERT(DATETIME, DOB),GETDATE()) AS AGE, DOB FROM MyTable` `

` `SET @Age = CAST(DATEDIFF(Year, @DOB, @Stamp) as int) IF (CAST(DATEDIFF(DAY, DATEADD(Year, @Age, @DOB), @Stamp) as int) < 0) SET @Age = @Age - 1` `

` `DECLARE @date datetime, @tmpdate datetime, @years int, @months int, @days int SELECT @date = '08/16/84' SELECT @tmpdate = @date SELECT @years = DATEDIFF(yy, @tmpdate, GETDATE()) - CASE WHEN (MONTH(@date) > MONTH(GETDATE())) OR (MONTH(@date) = MONTH(GETDATE()) AND DAY(@date) > DAY(GETDATE())) THEN 1 ELSE 0 END SELECT @tmpdate = DATEADD(yy, @years, @tmpdate) SELECT @months = DATEDIFF(m, @tmpdate, GETDATE()) - CASE WHEN DAY(@date) > DAY(GETDATE()) THEN 1 ELSE 0 END SELECT @tmpdate = DATEADD(m, @months, @tmpdate) SELECT @days = DATEDIFF(d, @tmpdate, GETDATE()) SELECT Convert(Varchar(Max),@years)+' Years '+ Convert(Varchar(max),@months) + ' Months '+Convert(Varchar(Max), @days)+'days'` `

` `declare @BirthDate datetime declare @ToDate datetime set @BirthDate = '1/3/1990' set @ToDate = '1/2/2008' select @BirthDate [Date of Birth], @ToDate [ToDate],(case when (DatePart(mm,@ToDate) < Datepart(mm,@BirthDate)) OR (DatePart(m,@ToDate) = Datepart(m,@BirthDate) AND DatePart(dd,@ToDate) < Datepart(dd,@BirthDate)) then (Datepart(yy, @ToDate) - Datepart(yy, @BirthDate) - 1) else (Datepart(yy, @ToDate) - Datepart(yy, @BirthDate))end) Age` `

` `DECLARE @dob datetime SET @dob='1992-01-09 00:00:00' SELECT DATEDIFF(YEAR, '0:0', getdate()-@dob)` `

` `DECLARE @D1 AS DATETIME, @D2 AS DATETIME SET @D2 = '2012-03-01 10:00:02' SET @D1 = '2013-03-01 10:00:01' SELECT DATEDIFF(YEAR, @D1,@D2) + CASE WHEN @D1<@D2 AND DATEADD(YEAR, DATEDIFF(YEAR,@D1, @D2), @D1) > @D2 THEN - 1 WHEN @D1>@D2 AND DATEADD(YEAR, DATEDIFF(YEAR,@D1, @D2), @D1) < @D2 THEN 1 ELSE 0 END AS AGE` `

` `declare @ReportStartDate datetime = CONVERT(datetime, '1/1/2014'), @DateofBirth datetime = CONVERT(datetime, '2/29/1948') FLOOR(DATEDIFF(HOUR,@DateofBirth,@ReportStartDate )/8766)` `

` `FLOOR(DATEDIFF(HOUR,@DateofBirth,@ReportStartDate )/8765.82) -- Divisor is more accurate than 8766` `

– 以下解决scheme给我更准确的结果。

` `FLOOR(DATEDIFF(YEAR,@DateofBirth,@ReportStartDate) - (CASE WHEN DATEADD(YY,DATEDIFF(YEAR,@DateofBirth,@ReportStartDate),@DateofBirth) > @ReportStartDate THEN 1 ELSE 0 END ))` `

` `Declare @dob datetime Declare @today datetime Set @dob = '05/20/2000' set @today = getdate() select CASE WHEN dateadd(year, datediff (year, @dob, @today), @dob) > @today THEN datediff (year, @dob, @today) - 1 ELSE datediff (year, @dob, @today) END as Age` `

` `select case when cast(getdate() as date) = cast(dateadd(year, (datediff(year, '1996-09-09', getdate())), '1996-09-09') as date) then dateDiff(yyyy,'1996-09-09',dateadd(year, 0, getdate())) else dateDiff(yyyy,'1996-09-09',dateadd(year, -1, getdate())) end as MemberAge go` `
` `CREATE function dbo.AgeAtDate( @DOB datetime, @CompareDate datetime ) returns INT as begin return CASE WHEN @DOB is null THEN null ELSE DateDiff(yy,@DOB, @CompareDate) - CASE WHEN datepart(mm,@CompareDate) > datepart(mm,@DOB) OR (datepart(mm,@CompareDate) = datepart(mm,@DOB) AND datepart(dd,@CompareDate) >= datepart(dd,@DOB)) THEN 0 ELSE 1 END END End GO` `

• 正确计算年龄
• 很短（大部分）
• （大部分）是非常可以理解的。

` `DECLARE @NOW DATETIME = '2013-07-04 23:59:59' DECLARE @DOB DATETIME = '1986-07-05'` `

` `DATEDIFF(YY, @DOB, @NOW) - CASE WHEN DATEADD(YY, DATEDIFF(YY, @DOB, @NOW), @DOB) > @NOW THEN 1 ELSE 0 END` `

` `DECLARE @AGE INT = DATEDIFF(YY, @DOB, @NOW) IF DATEADD(YY, @AGE, @DOB) > @NOW SET @AGE = @AGE - 1` `

` `DATEDIFF(YY, @DOB, @NOW) - CASE WHEN MONTH(@DOB) > MONTH(@NOW) OR MONTH(@DOB) = MONTH(@NOW) AND DAY(@DOB) > DAY(@NOW) THEN 1 ELSE 0 END` `

` `DATEDIFF(YY, @DOB, @NOW) - CEILING(0.5 * SIGN((MONTH(@DOB) - MONTH(@NOW)) * 50 + DAY(@DOB) - DAY(@NOW)))` `

` `DECLARE @FromDate DATETIME = '1992-01-2623:59:59.000', @ToDate DATETIME = '2016-08-10 00:00:00.000', @Years INT, @Months INT, @Days INT, @tmpFromDate DATETIME SET @Years = DATEDIFF(YEAR, @FromDate, @ToDate) - (CASE WHEN DATEADD(YEAR, DATEDIFF(YEAR, @FromDate, @ToDate), @FromDate) > @ToDate THEN 1 ELSE 0 END) SET @tmpFromDate = DATEADD(YEAR, @Years , @FromDate) SET @Months = DATEDIFF(MONTH, @tmpFromDate, @ToDate) - (CASE WHEN DATEADD(MONTH,DATEDIFF(MONTH, @tmpFromDate, @ToDate), @tmpFromDate) > @ToDate THEN 1 ELSE 0 END) SET @tmpFromDate = DATEADD(MONTH, @Months , @tmpFromDate) SET @Days = DATEDIFF(DAY, @tmpFromDate, @ToDate) - (CASE WHEN DATEADD(DAY, DATEDIFF(DAY, @tmpFromDate, @ToDate), @tmpFromDate) > @ToDate THEN 1 ELSE 0 END) SELECT @FromDate FromDate, @ToDate ToDate, @Years Years, @Months Months, @Days Days` `
` `CASE WHEN datepart(MM, getdate()) < datepart(MM, BIRTHDATE) THEN ((datepart(YYYY, getdate()) - datepart(YYYY, BIRTH_DATE)) -1 ) ELSE CASE WHEN datepart(MM, getdate()) = datepart(MM, BIRTHDATE) THEN CASE WHEN datepart(DD, getdate()) < datepart(DD, BIRTHDATE) THEN ((datepart(YYYY, getdate()) - datepart(YYYY, BIRTHDATE)) -1 ) ELSE (datepart(YYYY, getdate()) - datepart(YYYY, BIRTHDATE)) END ELSE (datepart(YYYY, getdate()) - datepart(YYYY, BIRTHDATE)) END END` `

` `ROUND(avg(CONVERT(int,DATEDIFF(hour,DOB,GETDATE())/8766.0)),0) AS AverageAge` `

` `select DATEDIFF(yy,@DATE,GETDATE()) - case when DATEPART(mm,GETDATE())*100+DATEPART(dd,GETDATE())>= DATEPART(mm,@DATE)*100+DATEPART(dd,@DATE) THEN 0 ELSE 1 END` `
` `SELECT CAST(DATEDIFF(dy, @DOB, GETDATE()+1)/365.25 AS int)` `
` `select datediff(day,'1991-03-16',getdate()) \\for days,get date refers today date select datediff(year,'1991-03-16',getdate()) \\for years select datediff(month,'1991-03-16',getdate()) \\for month` `

` `select cast(datediff(DAY, '2000-03-01 10:00:01', '2013-03-01 10:00:00') / (365.23076923074) as int) as 'Age'` `