How to use DATEDIFF to return year, month and day?

后端 未结 9 934
离开以前
离开以前 2020-12-10 22:36

How can I use DATEDIFF to return the difference between two dates in years, months and days in SQL Server 2005

DATEDIFF (date , dat         


        
相关标签:
9条回答
  • 2020-12-10 23:32
    Using ParseName
    
    DECLARE
      @ReportBeginDate DATE
    
    SET @ReportBeginDate='2015-01-01';
    
    IF OBJECT_ID('TEMPDB..#tmp_ymd') IS NOT NULL
    BEGIN
    DROP TABLE #tmp_ymd;
    END;
    
    select
    cast(cast(datediff(mm,@ReportBeginDate,getdate()) as decimal (10,2))/12 as decimal(10,2)) as YearMonthDec
    ,cast(datediff(dd,@ReportBeginDate,getdate()) as decimal (10,2)) as DayDec
    into #tmp_ymd
    
    select
    YearMonthDec
    ,cast(parsename(YearMonthDec,2) as decimal (10,0)) as yearnum
    ,cast(cast(parsename(YearMonthDec,1) as decimal (10,0))/100*(12) as numeric) as monthnum
    ,case when YearMonthDec>=1 then datediff(dd,CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(getdate())-1),getdate()),101),getdate()) else DayDec end as daynum
    
    from #tmp_ymd
    
    0 讨论(0)
  • 2020-12-10 23:33

    TL;DR approximates years & months without having to create a function.


    I found this question searching for "get years months from datediff". I was looking for something a bit quicker (and yeah, probably dirtier) than the above function-creating solutions.

    I came up with the following in-line sql which approximates the years and months. Sufficient for my own purposes, which is getting a count of events grouped by user's rough age at the time.

    select cast(datediff(event_datetime, dateofbirth)/365.25 as unsigned) as years, 
           cast((mod(datediff(event_datetime, dateofbirth),365.25))/30.4375 as unsigned) as months
    from tablename
    ;
    
    0 讨论(0)
  • 2020-12-10 23:36

    Check this page... http://www.sqlteam.com/article/datediff-function-demystified

    Create this functions:

    CREATE FUNCTION dbo.fnYearsApart
    (
            @FromDate DATETIME,
            @ToDate DATETIME
    )
    RETURNS INT
    AS
    BEGIN
            RETURN  CASE
                           WHEN @FromDate > @ToDate THEN NULL
                           WHEN DATEPART(day, @FromDate) > DATEPART(day, @ToDate) THEN DATEDIFF(month, @FromDate, @ToDate) - 1
                           ELSE DATEDIFF(month, @FromDate, @ToDate)
                   END / 12
    END
    

    and

    CREATE FUNCTION dbo.fnMonthsApart
    (
            @FromDate DATETIME,
            @ToDate DATETIME
    )
    RETURNS INT
    AS
    BEGIN
            RETURN  CASE
                           WHEN @FromDate > @ToDate THEN NULL
                           WHEN DATEPART(day, @FromDate) > DATEPART(day, @ToDate) THEN DATEDIFF(month, @FromDate, @ToDate) - 1
                           ELSE DATEDIFF(month, @FromDate, @ToDate)
                   END
    END
    

    and finally

    ALTER FUNCTION [dbo].[gatYMD](@dstart VARCHAR(50), @dend VARCHAR(50))
    RETURNS VARCHAR(50) AS
    BEGIN
        DECLARE @yy INT
        DECLARE @mm INT
        DECLARE @dd INT
        DECLARE @getmm INT
        DECLARE @getdd INT
    
        SET @yy = dbo.fnYearsApart(@dstart, @dend)  --DATEDIFF(yy, @dstart, @dend)
        SET @mm = dbo.fnMonthsApart(@dstart, @dend) --DATEDIFF(mm, @dstart, @dend)
        SET @dd = DATEDIFF(dd, @dstart, @dend)
        SET @getmm = ABS(DATEDIFF(mm, DATEADD(yy, @yy, @dstart), @dend))
        SET @getdd = ABS(DATEDIFF(dd, DATEADD(mm, DATEDIFF(mm, DATEADD(yy, @yy, @dstart), @dend), DATEADD(yy, @yy, @dstart)), @dend))
    
        RETURN (
          Convert(varchar(10),@yy) + ' años, ' + Convert(varchar(10),@getmm) + ' meses, '  + Convert(varchar(10),@getdd) + ' días'
        )
    END
    

    Greats!

    0 讨论(0)
提交回复
热议问题