检查时间范围是否覆盖Postgres中的now()

时间:2019-04-05 20:25:45

标签: sql postgresql

我有一个名为office_hours的列,其数据类型为timerange,它是通过以下方式定义的:

create type timerange as range (subtype = time)

我想编写一个查询,以检查该时间范围内是否包含now()。我正在寻找的东西类似于:

select now() <@ office_hours from users where office_hours is not null;

每当我尝试运行该查询时,我都会收到以下错误消息:

dbeaver error message

查询时间范围是否包含now()的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

该错误消息已经给出了很好的提示。由于您已经创建了time范围,因此您需要在检查是否包含之前将now()强制转换为时间。

考虑:

select now()::time <@ office_hours from users where office_hours is not null;

Demo on DB Fiddle

create type timerange as range (subtype = time);
select now(), now()::time <@ timerange('[20:00,23:00]') contained;

收益:

| now                      | contained |
| ------------------------ | --------- |
| 2019-04-05T20:54:13.113Z | true      |
相关问题