How to calculate age (in years) based on Date of Birth and getDate()

前端 未结 30 2058
死守一世寂寞
死守一世寂寞 2020-11-22 02:08

I have a table listing people along with their date of birth (currently a nvarchar(25))

How can I convert that to a date, and then calculate their age in years?

相关标签:
30条回答
  • 2020-11-22 02:22

    Try This

    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'
    
    0 讨论(0)
  • 2020-11-22 02:22

    Try this solution:

    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
    
    0 讨论(0)
  • 2020-11-22 02:23
    select floor((datediff(day,0,@today) - datediff(day,0,@birthdate)) / 365.2425) as age
    

    There are a lot of 365.25 answers here. Remember how leap years are defined:

    • Every four years
      • except every 100 years
        • except every 400 years
    0 讨论(0)
  • 2020-11-22 02:24

    The answer marked as correct is nearer to accuracy but, it fails in following scenario - where Year of birth is Leap year and day are after February month

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


    OR

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

    -- Following solution is giving me more accurate results.

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

    It worked in almost all scenarios, considering leap year, date as 29 feb, etc.

    Please correct me if this formula have any loophole.

    0 讨论(0)
  • 2020-11-22 02:27
    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
    
    0 讨论(0)
  • 2020-11-22 02:30

    So many of the above solutions are wrong DateDiff(yy,@Dob, @PassedDate) will not consider the month and day of both dates. Also taking the dart parts and comparing only works if they're properly ordered.

    THE FOLLOWING CODE WORKS AND IS VERY SIMPLE:

    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
    
    0 讨论(0)
提交回复
热议问题