简体   繁体   中英

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.

You can try below -

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

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')
SELECT * 
FROM mytable
WHERE startDate = TO_DATE('01/01/2019', 'MM/DD/YYYY') BETWEEN endDate = TO_DATE('01/01/2019', 'MM/DD/YYYY')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM