有没有一种简单的方法来计算两个日期之间的差异:
例如,2011年3月1日至2012年3月1日之间的差异为1年。但是,如果我使用DATEDIFF(day,..,..)并除以365,由于闰年,我得到1.00274的(不合需要的)答案。
要明确的是,我也需要任何年份分数(即不仅仅是整年)。例如,2011年3月1日至2012年3月3日之间的差异为1.005479(1年+ 2/365年)。 2011年3月1日至2012年2月29日之间的差异为0.997268(0年+ 365/366年)
因此,总而言之,上述两个例子中的输出(在DECIMAL(7,6)中)将是:
1.000000 1.005479 0.997268
答案 0 :(得分:4)
这是一个查询。但是您的示例中存在逻辑错误。我认为“2011年3月1日到2012年3月3日之间的差异是1.005479(1年+ 2/365年)”应该是(1年+ 2/366年)因为2012年3月3日到2012年3月3日之间的29.02.2012所以去年366天。
Declare @BDate datetime
Declare @EDate datetime
SET @BDate='2011-03-01'
SET @EDate='2012-02-29'
select
datediff(year,@BDate,@Edate)-
case when dateadd(year,datediff(year,@BDate,@Edate),@BDate)>@Edate then 1 else 0 end
+cast(datediff(day,dateadd(year,datediff(year,@BDate,@Edate)-
case when dateadd(year,datediff(year,@BDate,@Edate),@BDate)>@Edate then 1 else 0 end ,@BDate),@Edate) as float)/
cast(datediff(day,dateadd(year,-1,@Edate),@Edate) as float)
答案 1 :(得分:3)
我认为这是有效的(我希望选择你可以遵循的名字):
declare @StartTime datetime
declare @EndTime datetime
select @StartTime = '20110301',@EndTime = '20120303'
select YearsDiffNorm + ((DaysIntoYear * 1.0) / (DaysIntoYear + DaysRemainingInYear))
from (
select
YearsDiffNorm,
DATEDIFF(day,DATEADD(YEAR,YearsDiffNorm,@StartTime),@EndTime) as DaysIntoYear,
DATEDIFF(day,@EndTime,DATEADD(YEAR,YearsDiffNorm+1,@StartTime)) as DaysRemainingInYear
from (
select CASE WHEN DATEADD(year,YearsDiff,@StartTime) > @EndTime then YearsDiff - 1 else YearsDiff END as YearsDiffNorm
from (
select DATEDIFF(year,@StartTime,@EndTime) as YearsDiff
) t
) t2
) t3
答案 2 :(得分:0)
试试这个,
SELECT Cast(DateDiff(yyyy, '2011-03-01', '2012-03-01') As VARCHAR) + 'Yer : '
+ Cast(DateDiff(mm, '2011-03-01', '2012-03-01') As VARCHAR) + 'Mon : '
+ Cast(DateDiff(dd, '2011-03-01', '2012-03-01') As VARCHAR) + 'Dte'
希望这对你有所帮助,谢谢你。
答案 3 :(得分:0)
试试这个:
DECLARE @stdate datetime,@eddate datetime
SET @stdate='2007-02-01'
SET @eddate='2012-03-03'
;WITH CTE as (
select DATEDIFF(yy,@stdate,convert(datetime,cast(DATEPART(year,@eddate) as varchar)+'-'+cast(DATEPART(month,@stdate) as varchar)+'-'+cast(DATEPART(dd,@stdate) as varchar))) yrs,
DATEDIFF(dd,convert(datetime,cast(DATEPART(year,@eddate) as varchar)+'-'+cast(DATEPART(month,@stdate) as varchar)+'-'+cast(DATEPART(dd,@stdate) as varchar)),@eddate) as dayss,
CAST(CASE WHEN DATEPART(dd,DATEADD(mm,datediff(mm,-1,(convert(datetime,cast(DATEPART(year,@eddate) as varchar)+'-'+cast(DATEPART(month,@stdate) as varchar)+'-'+cast(DATEPART(dd,@stdate) as varchar)))),-1)) = 29 then 366 else 365 end as float) as ydays
)
select yrs+dayss/cast(ydays as float) from CTE
答案 4 :(得分:0)
DECLARE @FromDate DATETIME = '2019-02-28', @ToDate DATETIME = '2020-02-26',
@diff_year_temp int, @diff_year int,@diff_year_frac float,@ToDateDays float,@FromDateDays float,@y_to int,@y_from int,@diff float,
@FromDec float,@JanTo float,@JanToFrac float,@FromDecFrac float,@ToDateplusone datetime,@FromplusToyear datetime
set @diff_year_temp=datediff(year,@FromDate,@ToDate)
set @FromplusToyear=case when DATEPART(day,@FromDate)=29 and datepart(month,@FromDate)=2 then dateadd(day,1,dateadd(year,@diff_year_temp,@FromDate)) else dateadd(year,@diff_year_temp,@FromDate) end
set @ToDateplusone=dateadd(day,1,@ToDate)
set @y_to = datepart(year,@ToDate)
set @y_from=datepart(year,@FromDate)
set @FromDec=datediff(day,@FromDate,cast(CONCAT(@y_from,'-12-31') as date))+1
set @JanTo=datediff(day,cast(CONCAT(@y_to,'-01-01') as date),@ToDate)+1
set @ToDateDays = DATEDIFF(day, cast(@y_to as char(4)), cast(@y_to+1 as char(4)))
set @FromDateDays = DATEDIFF(day, cast(@y_from as char(4)), cast(@y_from+1 as char(4)))
set @FromDecFrac=@FromDec/@FromDateDays
set @JanToFrac=@JanTo/@ToDateDays
set @diff_year=case when @ToDateplusone >= @FromplusToyear then @diff_year_temp else @diff_year_temp-1 end
set @diff_year_frac=case when @ToDateplusone >= @FromplusToyear then datediff(day,@FromplusToyear,@ToDateplusone)/@ToDateDays else @JanToFrac+@FromDecFrac end
set @diff=@diff_year+@diff_year_frac
select @diff