אVי
אVי

Reputation: 1973

SQLite :select where currend date between from date and to date if exist

I have a db with records that have a fromDate and toDate field.

For getting the active record i use : datetime('now') between fromDate and toDate; That works fine.

The thing is that the fields are not always populated.

I want that if the is no fromDate and no toDate i will always receive the record

If there is only a fromDate then after that date i will always receive the record I

If ther is only a toDate i will receive the record only until the toDate.

Thanks a lot

Avi

Upvotes: 0

Views: 47

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269523

You can use logic like this:

where datetime('now') >= coalesce(fromDate, datetime('now'))
      datetime('now') <= coalesce(toDate, datetime('now'))

I don't recommend using between for date/time values. Time components can sometimes produce unexpected results.

Upvotes: 1

Related Questions