Denisa
Denisa

Reputation: 103

Get records between 2 dates but sometimes they can be null

I have 2 dates in database: start_date and end_date(type DATE). Sometimes end_date can be null, which means that if start_date <= CURRENT DATE I need that record as well. I added for now the condition for both dates set like NOW() >= of.start_date AND NOW() < of.end_date. How can I get the records which have end_date = NULL (in the same query), which means that they are always active if the start_date <= NOW() (they won't start in future)? There is also the possibility that neither start_date and end_date is set. In this case I also need those records as well. How can I achieve this in one query?

Upvotes: 0

Views: 145

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269943

Use coalesce() or put the NULL logic in explicitly:

where now() >= of.start_date and (of.end_date >= now() or of.end_date is null)

Upvotes: 1

Related Questions