SQL between dates

2019-08-14 12:34发布

问题:

I have table with two columns (startdate & enddate) and I have two submission dates (2012-02-29 & 2012-03-30) I am trying to get the data between startdate (2012-02-29) and the enddate (2012-03-30)

I've tried

 i.startdate between '2012-02-29' and i.enddate '2012-03-30'

but that didnt work, I've been googling this for hours with no clear answer, any help would be appreciated, thanks in advance.

回答1:

If I understand correctly:

  1. Your table contains two date fields, StartDate and EndDate, that describe a lengthy event.
  2. You want to find all the events that fall completely within the period described by two other dates, in this case 2012-02-29 and 2012-03-30.

If this is correct then use this:

 SELECT * FROM Events 
   WHERE StartDate >= '2012-02-29' AND EndDate <= '2012-03-30';


回答2:

SELECT * FROM table_with_dates WHERE (i.startdate  BETWEEN '2012-02-29' AND '2012-03-30') AND (i.enddate  BETWEEN '2012-02-29' AND '2012-03-30')  


回答3:

where ('2012-02-29' between i.startdate and i.enddate) and ('2012-03-30' between i.startdate and i.enddate)


回答4:

I understand that you want both your submissions (two dates) between startdate and enddate? so

Where (@firstDate between StartDate and EndDate) and (@secondDate between StartDate and EndDate)


回答5:

Since the startdate and enddate are separate fields and you are looking for specific startdates and enddates you don't really need a between, just a

select ..... from ..... where i.startdate >= '2012-02-29' and i.enddate =< '2012-03-30'