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

前端 未结 30 2090
死守一世寂寞
死守一世寂寞 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:43

    After trying MANY methods, this works 100% of the time using the modern MS SQL FORMAT function instead of convert to style 112. Either would work but this is the least code.

    Can anyone find a date combination which does not work? I don't think there is one :)

    --Set parameters, or choose from table.column instead:
    
    DECLARE @DOB    DATE = '2000/02/29' -- If @DOB is a leap day...
           ,@ToDate DATE = '2018/03/01' --...there birthday in this calculation will be 
    
    --0+ part tells SQL to calc the char(8) as numbers:
    SELECT [Age] = (0+ FORMAT(@ToDate,'yyyyMMdd') - FORMAT(@DOB,'yyyyMMdd') ) /10000
    
    0 讨论(0)
  • 2020-11-22 02:44

    Here is a technique which seems to work around the edge cases: the end of the year and leap Februaries.

    The problem with datediff(year,…,…) is that it only finds the difference between the years, and not the actual dates, which is a bit naïve in my opinion. This is only reliable if the date of birth is 1st Jan.

    The solution is to time warp the date of birth to the first of January, and the asking date by the same amount.

    Because of the leap year problem using the day of year is also unreliable, so I use the month and day to do the time warp:

    CREATE FUNCTION age(@then AS date,@now AS date) RETURNS int AS
    BEGIN
        DECLARE @month INT = month(@then)-1;
        DECLARE @day INT = day(@then)-1;
    
        SET @then=dateadd(month,-@month,@then);
        SET @then=dateadd(day,-@day,@then);
    
        SET @now=dateadd(month,-@month,@now;)
        SET @now=dateadd(day,-@day,@now);
    
        RETURN datediff(year,@then,@now);
    END;
    

    You can write this more compactly, of course. You can even write it in one line, if that’s your idea of having a good time:

    CREATE FUNCTION age(@then AS date,@now AS date) RETURNS int AS
    BEGIN
        RETURN  datediff(
            year,
            dateadd(day,-day(@then)+1,dateadd(month,-month(@then)+1,@then)),
            dateadd(day,-day(@then)+1,dateadd(month,-month(@then)+1,@now))
        );
    END;
    

    but that’s only showing off, and it’s less efficient as it needs to recalculate the offsets.

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

    source: http://beginsql.wordpress.com/2012/04/26/how-to-calculate-age-in-sql-server/

    0 讨论(0)
  • 2020-11-22 02:46

    How about this:

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

    We used something like here, but then taking the average age:

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

    Notice, the ROUND is outside rather than inside. This will allow for the AVG to be more accurate and we ROUND only once. Making it faster too.

    0 讨论(0)
  • 2020-11-22 02:48
    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
    
    0 讨论(0)
提交回复
热议问题