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. 不然会报错