使用一个 SQL 查询从特定日期和时间范围内选择信息
Selecting information from a specific range of days and hours using one SQL query
我想过滤从 4 月 1 日到 4 月 22 日的日期范围,但仅限于 8:00 下午和 6:00 上午之间 - 不在此范围内的其余时间应被排除.你对如何做到这一点有什么想法吗?到目前为止我唯一做的就是日期范围,但我不知道接下来如何处理它:
select * from cdm_service_request_logs
where inserted_at >= '20/04/01 20:00:00%'
and inserted_at < '20/04/22 06:00:00%'
ORDER BY ID DESC;
我想你想要这样的东西:
select srl.*
from cdm_service_request_logs srl
where inserted_at >= date '2020-04-01' and
inserted_at < date '2020-04-022' and
extract(hour from inserted_at) not between 6 and 19;
在Oracle中,时间比较经常使用字符串:
where inserted_at >= date '2020-04-01' and
inserted_at < date '2020-04-022' and
(to_char(inserted_at, 'HH24:MI') >= '20:00' or
to_char(inserted_at, 'HH24:MI') < '06:00'
)
请注意,日期比较使用日期常量值。这是对的。 %
对日期无效,因此您的比较是字符串。我只能推测您将 like
模式与日期常量混淆了。如果是这样,那只是被误导了。
我想过滤从 4 月 1 日到 4 月 22 日的日期范围,但仅限于 8:00 下午和 6:00 上午之间 - 不在此范围内的其余时间应被排除.你对如何做到这一点有什么想法吗?到目前为止我唯一做的就是日期范围,但我不知道接下来如何处理它:
select * from cdm_service_request_logs
where inserted_at >= '20/04/01 20:00:00%'
and inserted_at < '20/04/22 06:00:00%'
ORDER BY ID DESC;
我想你想要这样的东西:
select srl.*
from cdm_service_request_logs srl
where inserted_at >= date '2020-04-01' and
inserted_at < date '2020-04-022' and
extract(hour from inserted_at) not between 6 and 19;
在Oracle中,时间比较经常使用字符串:
where inserted_at >= date '2020-04-01' and
inserted_at < date '2020-04-022' and
(to_char(inserted_at, 'HH24:MI') >= '20:00' or
to_char(inserted_at, 'HH24:MI') < '06:00'
)
请注意,日期比较使用日期常量值。这是对的。 %
对日期无效,因此您的比较是字符串。我只能推测您将 like
模式与日期常量混淆了。如果是这样,那只是被误导了。