我有这个问题:
SELECT "visits".*
FROM "visits"
where
'2015-06-20' between start_date and end_date
or
if (end_date is null) then end_date = '9999-01-01'
我正在使用PostgreSQL 9.4。
问题是:
如何在一些行中测试日期是否也在没有end_date的范围内?
如果我使用1 = 1或类似的话, IF ELSE THEN
在Postgres中也不起作用......
怎么写呢?
答案 0 :(得分:2)
使用coalesce()
:
select *
from visits
where date '2015-06-20' between start_date and coalesce(end_date, date '9999-01-01');
或者为了使“结束”更加明显,您可以使用infinity
:
select *
from visits
where date '2015-06-20' between start_date and coalesce(end_date, 'infinity');
答案 1 :(得分:0)
使用COALESCE和OVERLAPS。
SELECT v.*
FROM visits
WHERE (start_date,COALESCE(end_date, '9999-01-01'::date)) OVERLAPS ('2015-06-20'::date,'2015-06-20'::date)