我的客户有这个KTV小时规则所有退房时间超过20分钟将被视为额外的1小时。
以下示例代码:
declare @dateStart as datetime, @dateEnd as datetime
set @dateStart = cast('3/2/2013 8:00:00 PM' as datetime)
set @dateEnd = cast('3/2/2013 9:20:02 pm' as datetime)
SELECT DATEDIFF(hour, @dateStart, @dateEnd)
上面的代码逻辑上返回1:20小时。在SQL Server中,它将返回1小时。如何调整此代码以返回2小时?
提前致谢。
答案 0 :(得分:4)
使用此:
declare @dateStart as datetime, @dateEnd as datetime
set @dateStart = cast('3/2/2013 8:00:00 PM' as datetime)
set @dateEnd = cast('3/2/2013 9:20:02 pm' as datetime)
SELECT CEILING(CAST( DATEDIFF(minute, @dateStart, @dateEnd)as float) / 60)
更多:CEILING
答案 1 :(得分:1)
试试这个:
declare @dateStart as datetime, @dateEnd as datetime
set @dateStart = cast('3/2/2013 8:00:00 PM' as datetime)
set @dateEnd = cast('3/2/2013 9:20:02 pm' as datetime)
SELECT DATEDIFF(hour, @dateStart, @dateEnd)
+
case
when datediff(minute,@dateStart, @dateEnd)%60>=20 then 1
else 0
end
更准确地说,你也可以在这里添加秒数。
答案 2 :(得分:0)
将40分钟添加到已用时间,并截断为小时,如下所示:
declare @dateStart as datetime, @dateEnd as datetime
set @dateStart = cast('3/2/2013 8:00:00 PM' as datetime)
set @dateEnd = cast('3/2/2013 9:20:02 pm' as datetime)
select Elapsed = datediff(hour, @dateStart, dateadd(minute,40,@dateEnd))
返回
Elapsed
-----------
2