DATEADD() 函数查找本工作日的值
如何使用 SQL DATEADD() 函数查找该工作日的记录?例如:
SELECT * FROM records r
WHERE r.creationTime <= GETDATE()
AND r.creationTime >= DATEADD(dd, -1 * hoursFromStartOfThisDay, GETDATE())
其中“hoursFromStartOfThisDay”正是它所说的。
注意:我说的是工作日,所以周一至周五上午 9 点至下午 5 点(如果有的话)。
谢谢你们!
编辑:我还希望在周/月的开始甚至小时的开始时应用一个解决方案。
编辑:因此,如果一条记录是在 18/07/2011 08:55:00 创建的,另一条记录是在 18/07/2011 09:05:00 创建的,我希望查询选取第二条记录,而不是第一条记录。但是,如果当前的 GETDATE() 是 18/07/2011 09:15:00 那么事情就会变得棘手。
添加 AND r.creationTime >= '9am'
怎么样?
How can I use the SQL DATEADD() function to find a record from this business day? For example:
SELECT * FROM records r
WHERE r.creationTime <= GETDATE()
AND r.creationTime >= DATEADD(dd, -1 * hoursFromStartOfThisDay, GETDATE())
Where 'hoursFromStartOfThisDay' is exactly what it says.
Note: I'm talking about business days so from 9am-5pm, Monday-Friday if that matters at all.
Thanks, guys!
Edit: I am also looking to apply a solution to this for the start of the beginning of the week/month and maybe hour too.
Edit: So if a record was created at 18/07/2011 08:55:00 and another created at 18/07/2011 09:05:00, I want the second record to be picked up by the query and not the first. However if the current GETDATE() is 18/07/2011 09:15:00 then it gets tricky.
how about adding AND r.creationTime >= '9am'
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您真的需要一个
hoursFromStartOfThisDay
变量吗?如果您的一天是固定的,从 9 点到 5 点,您可以使用以下静态范围:
Are you really need a
hoursFromStartOfThisDay
variable?If your day is static, from 9 to 5, you can use just this static range: