How to calculate age in T-SQL with years, months, and days

后端 未结 24 1543
无人共我
无人共我 2020-11-22 05:42

What would be the best way to calculate someone\'s age in years, months, and days in T-SQL (SQL Server 2000)?

The datediff function doesn\'t handle year

相关标签:
24条回答
  • 2020-11-22 06:05
    SELECT DOB AS Birthdate ,
           YEAR(GETDATE()) AS ThisYear,
           YEAR(getdate()) - YEAR(DOB) AS Age
    FROM tableprincejain
    
    0 讨论(0)
  • 2020-11-22 06:06

    There is an easy way, based on the hours between the two days BUT with the end date truncated.

    SELECT CAST(DATEDIFF(hour,Birthdate,CAST(GETDATE() as Date))/8766.0 as INT) AS Age FROM <YourTable>
    

    This one has proven to be extremely accurate and reliable. If it weren't for the inner CAST on the GETDATE() it might flip the birthday a few hours before midnight but, with the CAST, it is dead on with the age changing over at exactly midnight.

    0 讨论(0)
  • 2020-11-22 06:06

    Here is how I calculate the age given a birth date and the current date.

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

    Quite Old question, but I want to share what I have done to calculate age

        Declare @BirthDate As DateTime
    Set @BirthDate = '1994-11-02'
    
    SELECT DATEDIFF(YEAR,@BirthDate,GETDATE()) - (CASE 
    WHEN MONTH(@BirthDate)> MONTH(GETDATE()) THEN 1 
    WHEN MONTH(@BirthDate)= MONTH(GETDATE()) AND DAY(@BirthDate) > DAY(GETDATE()) THEN 1 
    Else 0 END)
    
    0 讨论(0)
  • 2020-11-22 06:09

    Here is some T-SQL that gives you the number of years, months, and days since the day specified in @date. It takes into account the fact that DATEDIFF() computes the difference without considering what month or day it is (so the month diff between 8/31 and 9/1 is 1 month) and handles that with a case statement that decrements the result where appropriate.

    DECLARE @date datetime, @tmpdate datetime, @years int, @months int, @days int
    SELECT @date = '2/29/04'
    
    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 @years, @months, @days
    
    0 讨论(0)
  • 2020-11-22 06:13
    declare @BirthDate datetime
    declare @TotalYear int
    declare @TotalMonths int
    declare @TotalDays int
    declare @TotalWeeks int
    declare @TotalHours int
    declare @TotalMinute int
    declare @TotalSecond int
    declare @CurrentDtTime datetime
    set @BirthDate='1998/01/05 05:04:00'  -- Set Your date here
    set @TotalYear= FLOOR(DATEDIFF(DAY, @BirthDate, GETDATE()) / 365.25)
    set @TotalMonths= FLOOR(DATEDIFF(DAY,DATEADD(year, @TotalYear,@BirthDate),GetDate()) / 30.436875E)
    set @TotalDays= FLOOR(DATEDIFF(DAY, DATEADD(month, @TotalMonths,DATEADD(year, 
        @TotalYear,@BirthDate)), GETDATE()))
    set @CurrentDtTime=CONVERT(datetime,CONVERT(varchar(50), DATEPART(year, 
        GetDate()))+'/' +CONVERT(varchar(50), DATEPART(MONTH, GetDate()))
        +'/'+ CONVERT(varchar(50),DATEPART(DAY, GetDate()))+' '
        + CONVERT(varchar(50),DATEPART(HOUR, @BirthDate))+':'+ 
         CONVERT(varchar(50),DATEPART(MINUTE, @BirthDate))+
       ':'+ CONVERT(varchar(50),DATEPART(Second, @BirthDate)))
    set @TotalHours = DATEDIFF(hour, @CurrentDtTime, GETDATE())
    if(@TotalHours < 0)
    begin
       set @TotalHours = DATEDIFF(hour,DATEADD(Day,-1, @CurrentDtTime), GETDATE())
       set @TotalDays= @TotalDays -1  
     end
    set @TotalMinute= DATEPART(MINUTE, GETDATE())-DATEPART(MINUTE, @BirthDate)
     if(@TotalMinute < 0)
    set @TotalMinute = DATEPART(MINUTE, DATEADD(hour,-1,GETDATE()))+(60-DATEPART(MINUTE, 
       @BirthDate))
    
    set @TotalSecond= DATEPART(Second, GETDATE())-DATEPART(Second, @BirthDate)
    
     Print 'Your age are'+ CHAR(13)
     + CONVERT(varchar(50), @TotalYear)+' Years, ' +
       CONVERT(varchar(50),@TotalMonths) +' Months, ' +
       CONVERT(varchar(50),@TotalDays)+' Days, ' +
       CONVERT(varchar(50),@TotalHours)+' Hours, ' +
       CONVERT(varchar(50),@TotalMinute)+' Minutes, ' + 
       CONVERT(varchar(50),@TotalSecond)+' Seconds. ' +char(13)+
         'Your are born at day of week was - ' + CONVERT(varchar(50),DATENAME(dw , 
         @BirthDate ))
      +char(13)+char(13)+
    +'Your Birthdate to till date your '+ CHAR(13)
    +'Years - ' + CONVERT(varchar(50), FLOOR(DATEDIFF(DAY, @BirthDate, GETDATE()) / 
       365.25))
    +' , Months - ' + CONVERT(varchar(50),DATEDIFF(MM,@BirthDate,getdate())) 
    +' , Weeks - ' + CONVERT(varchar(50),DATEDIFF(wk,@BirthDate,getdate()))
    +' , Days - ' + CONVERT(varchar(50),DATEDIFF(dd,@BirthDate,getdate()))+char(13)+
    +'Hours - ' + CONVERT(varchar(50),DATEDIFF(HH,@BirthDate,getdate()))
    +' , Minutes - ' + CONVERT(varchar(50),DATEDIFF(mi,@BirthDate,getdate()))
    +' , Seconds - ' + CONVERT(varchar(50),DATEDIFF(ss,@BirthDate,getdate()))
    

    Output

    Your age are
    22 Years, 0 Months, 2 Days, 11 Hours, 30 Minutes, 16 Seconds. 
    Your are born at day of week was - Monday
    
    Your Birthdate to till date your 
    Years - 22 , Months - 264 , Weeks - 1148 , Days - 8037
    Hours - 192899 , Minutes - 11573970 , Seconds - 694438216
    
    0 讨论(0)
提交回复
热议问题