这里,我已经有了一个工作的T-SQL(SQL Server)的解决方案(基于测试至今)。可悲的是我没有一个Oracle实例上播放,但希望这不应该是太难转换:
declare @fromDate date = '2010-11-21'
, @toDate date = getutcdate()
--from date must be before to date
declare @tempDate date
if @toDate < @fromDate
begin
set @tempDate = @toDate
set @toDate = @fromDate
set @fromDate = @tempDate
end
declare @fDD int = datepart(dd,@fromdate)
, @tDD int = datepart(dd,@todate)
, @fMM int = datepart(mm,@fromdate)
, @tMM int = datepart(mm,@todate)
, @fYYYY int = datepart(yyyy,@fromdate)
, @tYYYY int = datepart(yyyy,@todate)
, @y int, @m int, @d int
--calc difference in years
set @y = @[email protected]
if @fMM > @tMM or (@[email protected] and @fDD > @tDD)
begin
set @y = @y - 1
set @fYYYY = @fYYYY + @y
set @tempDate = DATEADD(year,@y,@fromDate)
end
--calc remaining difference in months
set @m = DATEDIFF(month,@tempDate,@toDate)
if @tDD < @fDD
begin
set @m = @m-1
set @tempDate = DATEADD(month,@m,@tempDate)
end
--calc remaining difference in days
set @d = DATEDIFF(day,@tempDate,@toDate)
--display results in user friendly and grammatically correct way
select cast(@y as nvarchar(3)) + N' year' + case when @y = 1 then N'' else N's' end + N' '
+ cast(@m as nvarchar(2)) + N' month' + case when @m = 1 then N'' else N's' end + N' '
+ cast(@d as nvarchar(3)) + N' day' + case when @d = 1 then N'' else N's' end + N' '
请加结构的表格,样本记录和您想要的输出。 :) –
告诉我们你已经尝试过了。谷歌它之前你Stackoverflow它! (我刚发明了一种新的动词!) –