sqlserver获取两个时间差,输出天时分秒
创建标量函数
CREATE FUNCTION [dbo].[getddhhmmss]
(
@starttime DATETIME ,
@endtime DATETIME
)
RETURNS varchar(100)
AS
begin
declare @wxdiff varchar(100)
DECLARE @datediffss INT = DATEDIFF(ss, @starttime, @endtime);
set @wxdiff=( CAST(CAST(@datediffss / (60*60*24) AS INT) AS VARCHAR) + '天'
+ CAST(CAST(@datediffss % 86400 / 3600 AS INT) AS VARCHAR) + '小时'
+ CAST(CAST(@datediffss % 3600 / 60 AS INT) AS VARCHAR) + '分'
+ CAST(CAST(@datediffss % 60 AS INT) AS VARCHAR) + '秒')
set @wxdiff=cast(@wxdiff as varchar(50))
RETURN isnull(@wxdiff,'')
END
调用: