SQL Server 中的 DateDiff 舍入
我有一个表,其中包含运行特定函数的最后日期/时间。在我的存储过程中,如果当前日期/时间距上次运行时间超过一个小时,我想返回 1;如果小于,则返回 0。目前我有这个:
IF((SELECT TOP 1 DATEDIFF(HH,LastRunDateTime,GETDATE()) FROM myTable) >= 1)
BEGIN
-- run statement to update LastRunDateTime
return 1;
END
ELSE
return 0;
END
How does round work with DateDiff?
有没有更好的方法,如果已经超过一个小时,则只返回 1 ?
I have a table that contains the last date/time a particular function was ran. In my stored procedure, I want to return 1 if the current date/time is more than an hour away from the last time it was ran and 0 if it was less. Currently I have this:
IF((SELECT TOP 1 DATEDIFF(HH,LastRunDateTime,GETDATE()) FROM myTable) >= 1)
BEGIN
-- run statement to update LastRunDateTime
return 1;
END
ELSE
return 0;
END
How does rounding work with DateDiff?
Is there a better way to only return 1 if it has been more than an hour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用:
参考:
提供的文档链接中的日期部分边界部分列出了此类信息。
Use:
Reference:
The datepart Boundaries section in the documentation link provided lists such information.
我认为您希望
DateDiff 比仅仅减去两个日期时间更微妙。它实际上告诉你两者之间跨越了多少“界限”。例如:
打印以下内容:
这很令人困惑,因为第二对日期时间相距较远。当您需要时,此功能非常有用,但当您不需要时,它就违反直觉。
I think you want
DateDiff is a little more subtle than just subtracting two datetimes. It actually tells you how many "boundaries" are crossed between the two. For example:
prints the following:
which is confusing, because the second pair of datetimes are farther apart. This functionality is great when you need it, but counterintuitive when you don't.