1

我的客户有这个 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 小时?

提前致谢。

4

3 回答 3

5

用这个:

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)

更多:天花板

于 2013-03-12T06:20:21.440 回答
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

更准确地说,您还可以在此处添加秒数。

于 2013-03-12T06:26:48.197 回答
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
于 2013-03-12T06:24:09.057 回答