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

后端 未结 24 1571
无人共我
无人共我 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:14

    Simple way to get age as text is as below:

    Select cast((DATEDIFF(m, date_of_birth, GETDATE())/12) as varchar) + ' Y & ' + 
           cast((DATEDIFF(m, date_of_birth, GETDATE())%12) as varchar) + ' M' as Age
    

    Results Format will be:

    **63 Y & 2 M**
    
    0 讨论(0)
  • 2020-11-22 06:14

    The same sort of thing as a function.

    create function [dbo].[Age](@dayOfBirth datetime, @today datetime)
       RETURNS varchar(100)
    AS
    
    Begin
    DECLARE @thisYearBirthDay datetime
    DECLARE @years int, @months int, @days int
    
    set @thisYearBirthDay = DATEADD(year, DATEDIFF(year, @dayOfBirth, @today), @dayOfBirth)
    set @years = DATEDIFF(year, @dayOfBirth, @today) - (CASE WHEN @thisYearBirthDay > @today THEN 1 ELSE 0 END)
    set @months = MONTH(@today - @thisYearBirthDay) - 1
    set @days = DAY(@today - @thisYearBirthDay) - 1
    
    return cast(@years as varchar(2)) + ' years,' + cast(@months as varchar(2)) + ' months,' + cast(@days as varchar(3)) + ' days'
    end
    
    0 讨论(0)
  • 2020-11-22 06:14

    There is another method for calculate age is

    See below table

        FirstName       LastName    DOB
        sai             krishnan    1991-11-04
        Harish          S A         1998-10-11
    

    For finding age,you can calculate through month

      Select datediff(MONTH,DOB,getdate())/12 as dates from [Organization].[Employee]
    

    Result will be

    firstname   dates
    sai         27
    Harish      20
    
    0 讨论(0)
  • 2020-11-22 06:15
    CREATE FUNCTION DBO.GET_AGE
    (
    @DATE AS DATETIME
    )
    RETURNS VARCHAR(MAX)
    AS
    BEGIN
    
    DECLARE @YEAR  AS VARCHAR(50) = ''
    DECLARE @MONTH AS VARCHAR(50) = ''
    DECLARE @DAYS  AS VARCHAR(50) = ''
    DECLARE @RESULT AS VARCHAR(MAX) = ''
    
    SET @YEAR  = CONVERT(VARCHAR,(SELECT DATEDIFF(MONTH,CASE WHEN DAY(@DATE) > DAY(GETDATE()) THEN DATEADD(MONTH,1,@DATE) ELSE @DATE END,GETDATE()) / 12 ))
    SET @MONTH = CONVERT(VARCHAR,(SELECT DATEDIFF(MONTH,CASE WHEN DAY(@DATE) > DAY(GETDATE()) THEN DATEADD(MONTH,1,@DATE) ELSE @DATE END,GETDATE()) % 12 ))
    SET @DAYS = DATEDIFF(DD,DATEADD(MM,CONVERT(INT,CONVERT(INT,@YEAR)*12 + CONVERT(INT,@MONTH)),@DATE),GETDATE())
    
    SET @RESULT = (RIGHT('00' + @YEAR, 2) + ' YEARS ' + RIGHT('00' + @MONTH, 2) + ' MONTHS ' + RIGHT('00' + @DAYS, 2) + ' DAYS')
    
    RETURN @RESULT
    END
    
    SELECT DBO.GET_AGE('04/12/1986')
    
    0 讨论(0)
  • 2020-11-22 06:15
    DECLARE @BirthDate datetime, @AgeInMonths int
    SET @BirthDate = '10/5/1971'
    SET @AgeInMonths                              -- Determine the age in "months old":
        = DATEDIFF(MONTH, @BirthDate, GETDATE())  -- .Get the difference in months
        - CASE WHEN DATEPART(DAY,GETDATE())       -- .If today was the 1st to 4th,
                  < DATEPART(DAY,@BirthDate)      --   (or before the birth day of month)
               THEN 1 ELSE 0 END                  --   ... don't count the month.
    SELECT @AgeInMonths / 12 as AgeYrs            -- Divide by 12 months to get the age in years
          ,@AgeInMonths % 12 as AgeXtraMonths     -- Get the remainder of dividing by 12 months = extra months
          ,DATEDIFF(DAY                           -- For the extra days, find the difference between, 
                   ,DATEADD(MONTH, @AgeInMonths   -- 1. Last Monthly Birthday 
                                 , @BirthDate)    --     (if birthdays were celebrated monthly)
                   ,GETDATE()) as AgeXtraDays     -- 2. Today's date.
    
    0 讨论(0)
  • 2020-11-22 06:16
    declare @StartDate datetime = '2016-01-31'
    declare @EndDate datetime = '2016-02-01'
    SELECT @StartDate AS [StartDate]
          ,@EndDate AS [EndDate]
          ,DATEDIFF(Year,@StartDate,@EndDate) - CASE WHEN DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate), @StartDate) > @EndDate THEN 1 ELSE 0 END AS [Years]
          ,DATEDIFF(Month,(DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate) - CASE WHEN DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate), @StartDate) > @EndDate THEN 1 ELSE 0 END,@StartDate)),@EndDate) - CASE WHEN DATEADD(Month, DATEDIFF(Month,DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate) - CASE WHEN DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate), @StartDate) > @EndDate THEN 1 ELSE 0 END,@StartDate),@EndDate) , @StartDate) > @EndDate THEN 1 ELSE 0 END AS [Months]
          ,DATEDIFF(Day, DATEADD(Month,DATEDIFF(Month, (DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate) - CASE WHEN DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate), @StartDate) > @EndDate THEN 1 ELSE 0 END,@StartDate)),@EndDate) - CASE WHEN DATEADD(Month, DATEDIFF(Month,DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate) - CASE WHEN DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate), @StartDate) > @EndDate THEN 1 ELSE 0 END,@StartDate),@EndDate) , @StartDate) > @EndDate THEN 1 ELSE 0 END  ,DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate) - CASE WHEN DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate), @StartDate) > @EndDate THEN 1 ELSE 0 END,@StartDate)) ,@EndDate) - CASE WHEN DATEADD(Day,DATEDIFF(Day, DATEADD(Month,DATEDIFF(Month, (DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate) - CASE WHEN DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate), @StartDate) > @EndDate THEN 1 ELSE 0 END,@StartDate)),@EndDate) - CASE WHEN DATEADD(Month, DATEDIFF(Month,DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate) - CASE WHEN DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate), @StartDate) > @EndDate THEN 1 ELSE 0 END,@StartDate),@EndDate) , @StartDate) > @EndDate THEN 1 ELSE 0 END  ,DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate) - CASE WHEN DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate), @StartDate) > @EndDate THEN 1 ELSE 0 END,@StartDate)) ,@EndDate),DATEADD(Month,DATEDIFF(Month, (DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate) - CASE WHEN DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate), @StartDate) > @EndDate THEN 1 ELSE 0 END,@StartDate)),@EndDate) - CASE WHEN DATEADD(Month, DATEDIFF(Month,DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate) - CASE WHEN DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate), @StartDate) > @EndDate THEN 1 ELSE 0 END,@StartDate),@EndDate) , @StartDate) > @EndDate THEN 1 ELSE 0 END  ,DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate) - CASE WHEN DATEADD(Year,DATEDIFF(Year,@StartDate,@EndDate), @StartDate) > @EndDate THEN 1 ELSE 0 END,@StartDate))) > @EndDate THEN 1 ELSE 0 END AS [Days]
    
    0 讨论(0)
提交回复
热议问题