我想在00-00-00 00:00:00得到日期差异 当我减去2个日期时,我得到的结果总是从1900-01-01开始。
如何才能显示剩余的日期和时间。 或甚至表示(X)天(X)小时(X)分钟
SELECT TOP
[Open_Date]
,[Close_Date]
,([Open_Date]-[Close_Date]) as [test]
,[time_spent_sum]
,[support_lev]
,[Priority]
,[CountCases]
,[Last_Update_Status]
,[Last_Update_Date]
,[Customer Group]
,[Assignee Group]
,[3rd_Party]
,[rootcause]
,[location_name]
,[Dataline]
,[InterfaceID]
,[DAYS OPEN]
FROM [mdb].[dbo].[ReportsView]
我一直在捣乱一些皈依者,但没有达到预期的效果。
case
when [Close_Date] is null then format(convert(smalldatetime, dateadd(minute, (datediff(minute,[Open_Date],getdate())), ''), 114),'d ''days'' HH ''Hrs'' :mm ''Mins'' ')
when [Close_Date] is not null then format(convert(smalldatetime, dateadd(minute, (datediff(minute,[Open_Date],[Close_Date])), 0), 114),'d ''days'' HH ''Hrs'' :mm ''Mins'' ')
else null
end as [Total_call_open_time]
答案 0 :(得分:0)
为了更准确地表示两个时间段之间的天数/小时/分钟/秒,您需要找到两个日期之间的实际时间,因为datediff
实际上告诉您跨越了多少个边界。
例如,即使两个时期差异接近48小时,以下两者都会返回值1:
select datediff(day,'2000-01-01 00:00:01','2000-01-02 23:59:59')
select datediff(day,'2000-01-01 23:59:59','2000-01-02 00:00:01')
因此,以下内容可以满足您的需求,使用div /
和mod %
来计算总时间跨度和剩余时间:
declare @d1 datetime = dateadd(day,-1,dateadd(hour,-2,dateadd(minute,-3,dateadd(second,-4,getdate()))))
declare @d2 datetime = getdate()
select datediff(hour,@d1,@d2)/24 as Days
,datediff(minute,@d1,@d2)/60%24 as Hours
,datediff(second,@d1,@d2)/60%60 as Minutes
,datediff(second,@d1,@d2)%60%60 as Seconds
,cast(datediff(hour,@d1,@d2)/24 as nvarchar(3)) + case when cast(datediff(day,@d1,@d2) as nvarchar(3)) = 1 then ' Day, ' else ' Days, ' end
+ cast(datediff(minute,@d1,@d2)/60%24 as nvarchar(3)) + case when cast(datediff(hour,@d1,@d2)%24 as nvarchar(3)) = 1 then ' Hour, ' else ' Hours, ' end
+ cast(datediff(second,@d1,@d2)/60%60 as nvarchar(3)) + case when cast(datediff(Minute,@d1,@d2)%60 as nvarchar(3)) = 1 then ' Minute, ' else ' Minutes, ' end
+ cast(datediff(Second,@d1,@d2)%60%60 as nvarchar(3)) + case when cast(datediff(Second,@d1,@d2)%60%60 as nvarchar(3)) = 1 then ' Second' else ' Seconds' end as TimeDifference