SQL计算时间差,要精确到小时分钟秒

SQL计算时间差,要精确到小时分钟秒
declare @starttime as datetime
declare @endtime as datetime
set @starttime = '2009-11-24 15:10:00' 
set @endtime = '2009-11-24 15:12:24'
select right('00'+cast(cast(datediff(ss,@starttime,@endtime)/3600 as int) as varchar),2)+':'+
       right('00'+cast(cast(datediff(ss,@starttime,@endtime)%3600/60 as int) as varchar),2)+':'+
       right('00'+cast(cast(datediff(ss,@starttime,@endtime)%60 as int) as varchar),2) as TimeEd

定义为函数

create FUNCTION [f_TimeBetween]
(
@start datetime,
@end datetime
)
RETURNS varchar(10)
AS
BEGIN
    RETURN right('00'+cast(cast(datediff(ss,@start,@end)/3600 as int) as varchar),2)+':'+
   right('00'+cast(cast(datediff(ss,@start,@end)%3600/60 as int) as varchar),2)+':'+
   right('00'+cast(cast(datediff(ss,@start,@end)%60 as int) as varchar),2)
END

 调用

declare @starttime as datetime
declare @endtime as datetime
set @starttime = '2009-11-24 15:10:00' 
set @endtime = '2009-11-24 15:12:24'
select DBO.f_TimeBetween(@starttime,@endtime)

一定要加上DBO.   不然会报错

posted on 2016-11-02 15:10 hllive 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/hllive/p/6022989.html

你可能感兴趣的:(SQL计算时间差,要精确到小时分钟秒)