基于当前日期和日期的条件查询时间
我有一个 SQL 查询,需要从表中选择时间在昨天下午 3:00 到今天下午 3:00 之间的记录如果今天的时间超过下午 3:00。
如果今天的时间小于该时间,例如今天的时间是下午 1:00。那么我的查询应该将今天的时间设为下午 1:00(这应该返回我的记录)。
如果今天的时间超过下午 3:00,我需要获取昨天下午 3:00 到今天下午 3:00 之间的时间 如果今天的时间小于 3:00 PM,则获取昨天下午 3:00 到今天的当前时间
I have an sql query in which I need to select the records from table where the time is between 3:00 PM yesterday to 3:00 PM today if today's time is more than 3:00 PM.
If today's time is less than that, like if today's time is 1:00 PM. then then my query should take today's time as 1:00 PM (which should return me records).
I need to get the time between 3:00pm yesterday to 3:00pm today if todays time is more than 3:00pm
if todays time is less than 3:00pm then get the 3:00pm yesterday to current time today
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
处理此问题的最佳方法是使用 IF 语句:
条件 WHERE 子句是不可控制的。
以前:
如果我理解正确的话,你想要获取最后一天内的记录。如果当前时间为下午 3 点或更晚,则时间应设置为下午 3 点。如果早于下午 3 点,则取当前时间...
注意:
dt - 1
表示将从 Oracle DATE 中减去 24 小时。参考:
The best way of handling this is to use an IF statement:
Conditional WHERE clauses are non-sargable.
Previously:
If I understand correctly, you want to get records within the last day. If the current time is 3 PM or later, the time should be set to 3 PM. If earlier than 3 PM, take the current time...
Note:
dt - 1
means that 24 hours will be subtracted from the Oracle DATE.Reference:
不需要 IF 语句。这个问题可以通过简单的 SQL 轻松解决。
我的表 T23 有一些带日期的记录;这是一个时间为下午 3 点的示例:
由于当前时间在下午 3 点之前,我的查询将返回 5 月 17 日和 5 月 18 日的记录,但不会返回 ID=62 的记录...
There is no need for an IF statement. This can be solved easily with simple SQL.
My table T23 has some records with dates; here is a sample with times at 3.00pm:
As the current time is before 3.00pm my query will return records from 17-MAY and 18-MAY but not the record where ID=62...