Samuel Smith
Samuel Smith

Reputation: 149

How do I select rows where today's date falls in between start and end date inclusive

So I have 2 columns of data, one startdate and enddate. I want to select rows where entries have start date and/or end date falls on today's date. Refer to diagram below.

Upvotes: 3

Views: 703

Answers (3)

Rchl Sy
Rchl Sy

Reputation: 25

SELECT * 
FROM mytable
WHERE startDate = TO_DATE('01/01/2019', 'MM/DD/YYYY') BETWEEN endDate = TO_DATE('01/01/2019', 'MM/DD/YYYY')

Upvotes: 0

Bruno Smaldone
Bruno Smaldone

Reputation: 210

You need a WHERE clause that will take both columns into account, using an OR condition, so if any of the two are true, the condition validates.

Like this:

SELECT * 
FROM mytable
WHERE startDate = '2019-01-01' OR endDate = '2019-01-01'

Now, if startDate and endDate are just dates, that would work. But if startDate and endDate are timestamps (full dates plus hours-min-sec..) then it won't be a match.

You'd need to parse the date, like this:

WHERE Date_Format(startDate, '%y-%m-%d') = '2019-01-01' 
OR Date_Format(startDate, '%y-%m-%d') = '2019-01-01'

Should you want to use the current date instead of a given date:

WHERE Date_Format(startDate, '%y-%m-%d') = Date_Format(NOW(), '%y-%m-%d')
OR Date_Format(startDate, '%y-%m-%d') = Date_Format(NOW(), '%y-%m-%d')

Upvotes: 0

Fahmi
Fahmi

Reputation: 37473

You can try below -

select * from tablename
where startdate>=date(now()) and endate<=date(now())

Upvotes: 3

Related Questions