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

后端 未结 24 1569
无人共我
无人共我 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 05:56
    select DOB as Birthdate,
           YEAR(GETDATE()) as ThisYear, 
           YEAR(getdate()) - EAR(date1) as Age   
    from TableName
    
    0 讨论(0)
  • 2020-11-22 05:57

    Here is a (slightly) simpler version:

    CREATE PROCEDURE dbo.CalculateAge 
        @dayOfBirth datetime
    AS
    
    DECLARE @today datetime, @thisYearBirthDay datetime
    DECLARE @years int, @months int, @days int
    
    SELECT @today = GETDATE()
    
    SELECT @thisYearBirthDay = DATEADD(year, DATEDIFF(year, @dayOfBirth, @today), @dayOfBirth)
    
    SELECT @years = DATEDIFF(year, @dayOfBirth, @today) - (CASE WHEN @thisYearBirthDay > @today THEN 1 ELSE 0 END)
    
    SELECT @months = MONTH(@today - @thisYearBirthDay) - 1
    
    SELECT @days = DAY(@today - @thisYearBirthDay) - 1
    
    SELECT @years, @months, @days
    GO
    
    0 讨论(0)
  • 2020-11-22 05:59
    DECLARE @DoB AS DATE = '1968-10-24'
    DECLARE @cDate AS DATE = CAST('2000-10-23' AS DATE)
    
    SELECT 
    --Get Year difference
    DATEDIFF(YEAR,@DoB,@cDate) -
    --Cases where year difference will be augmented
    CASE 
        --If Date of Birth greater than date passed return 0
        WHEN YEAR(@DoB) - YEAR(@cDate) >= 0 THEN DATEDIFF(YEAR,@DoB,@cDate)
    
        --If date of birth month less than date passed subtract one year
        WHEN MONTH(@DoB) - MONTH(@cDate) > 0 THEN 1 
    
        --If date of birth day less than date passed subtract one year
        WHEN MONTH(@DoB) - MONTH(@cDate) = 0 AND DAY(@DoB) - DAY(@cDate) > 0 THEN 1 
    
        --All cases passed subtract zero
        ELSE 0
    END
    
    0 讨论(0)
  • 2020-11-22 06:02

    Try this...

    SELECT CASE WHEN
     (DATEADD(year,DATEDIFF(year, @datestart  ,@dateend) , @datestart) > @dateend)
    THEN DATEDIFF(year, @datestart  ,@dateend) -1
    ELSE DATEDIFF(year, @datestart  ,@dateend)
    END
    

    Basically the "DateDiff( year...", gives you the age the person will turn this year, so i have just add a case statement to say, if they have not had a birthday yet this year, then subtract 1 year, else return the value.

    0 讨论(0)
  • 2020-11-22 06:03
    create  procedure getDatedifference
    
    (
        @startdate datetime,
        @enddate datetime
    )
    as
    begin
        declare @monthToShow int
        declare @dayToShow int
    
        --set @startdate='01/21/1934'
        --set @enddate=getdate()
    
        if (DAY(@startdate) > DAY(@enddate))
            begin
                set @dayToShow=0
    
                if (month(@startdate) > month(@enddate))
                    begin
                        set @monthToShow=  (12-month(@startdate)+ month(@enddate)-1)
                    end
                else if (month(@startdate) < month(@enddate))
                    begin
                        set @monthToShow=  ((month(@enddate)-month(@startdate))-1)
                    end
                else
                   begin
                       set @monthToShow=  11
                   end
                -- set @monthToShow= convert(int, DATEDIFF(mm,0,DATEADD(dd,DATEDIFF(dd,0,@enddate)- DATEDIFF(dd,0,@startdate),0)))-((convert(int,FLOOR(DATEDIFF(day, @startdate, @enddate) / 365.25))*12))-1
                             if(@monthToShow<0)
                             begin
                                set @monthToShow=0
                             end
    
                          declare @amonthbefore integer
                          set @amonthbefore=Month(@enddate)-1
                              if(@amonthbefore=0)
                                 begin
                                    set @amonthbefore=12
                                  end
    
    
                          if (@amonthbefore  in(1,3,5,7,8,10,12))
                              begin
                                set @dayToShow=31-DAY(@startdate)+DAY(@enddate)
                              end
                          if (@amonthbefore=2)
                             begin
                               IF (YEAR( @enddate ) % 4 = 0 AND YEAR( @enddate ) % 100 != 0) OR  YEAR( @enddate ) % 400 = 0
                                     begin
                                        set @dayToShow=29-DAY(@startdate)+DAY(@enddate)
                                      end
                               else
                                   begin
                                       set @dayToShow=28-DAY(@startdate)+DAY(@enddate)
                               end
                          end
                          if (@amonthbefore in (4,6,9,11))
                            begin
                               set @dayToShow=30-DAY(@startdate)+DAY(@enddate)
                            end
                     end
        else
            begin
              --set @monthToShow=convert(int, DATEDIFF(mm,0,DATEADD(dd,DATEDIFF(dd,0,@enddate)- DATEDIFF(dd,0,@startdate),0)))-((convert(int,FLOOR(DATEDIFF(day, @startdate, @enddate) / 365.25))*12))
              if (month(@enddate)< month(@startdate))
                  begin
                     set @monthToShow=12+(month(@enddate)-month(@startdate))
                  end
              else
                  begin
                    set @monthToShow= (month(@enddate)-month(@startdate))
                  end
              set @dayToShow=DAY(@enddate)-DAY(@startdate)
            end
    
        SELECT
            FLOOR(DATEDIFF(day, @startdate, @enddate) / 365.25) as [yearToShow],
              @monthToShow as  monthToShow ,@dayToShow as dayToShow ,
            convert(varchar,FLOOR(DATEDIFF(day, @startdate, @enddate) / 365.25)) +' Year ' + convert(varchar,@monthToShow) +' months '+convert(varchar,@dayToShow)+' days ' as age
    
        return
    end
    
    0 讨论(0)
  • 2020-11-22 06:04

    Here is SQL code that gives you the number of years, months, and days since the sysdate. Enter value for input_birth_date this format(dd_mon_yy). note: input same value(birth date) for years, months & days such as 01-mar-85

    select trunc((sysdate -to_date('&input_birth_date_dd_mon_yy'))/365) years,
    trunc(mod(( sysdate -to_date('&input_birth_date_dd_mon_yy'))/365,1)*12) months,
    trunc((mod((mod((sysdate -to_date('&input_birth_date_dd_mon_yy'))/365,1)*12),1)*30)+1) days 
     from dual
    
    0 讨论(0)
提交回复
热议问题