sql 日期相减带小数 急!!!

2025-01-01 15:15:21
推荐回答(2个)
回答1:

DECLARE @dt1 AS datetime, @dt2 AS datetime;
SELECT @dt1 = '2008-8-4 9:36:41', @dt2 = '2008-8-2 9:33:39';

DECLARE @Days AS int, @Hours AS int, @Minutes AS int, @Seconds AS int;

SET @Seconds = DATEDIFF( second, @dt2, @dt1);
SET @Days = @Seconds / (24 * 60 * 60)
SET @Seconds = @Seconds - @Days * 24 * 60 * 60
SET @Hours = @Seconds / (60 * 60);
SET @Seconds = @Seconds - @Hours * 60 * 60
SET @Minutes = @Seconds / 60;
SET @Seconds = @Seconds - @Minutes * 60;
SELECT CONVERT(varchar(10), @Days ) + '天' + CONVERT(varchar(10), @Hours ) + '小时' + CONVERT(varchar(10), @Minutes ) + '分' + CONVERT(varchar(10), @Seconds ) + '秒';

回答2:

例如比较'2010-1-1 00:01:00'和'2010-1-2 03:03:00'的日期差
select DATEDIFF (s,'2010-1-1 00:01:00', '2010-1-2 03:03:00')/(60.0*60.0*24.0)

---以下为结果:
1.126388888888