SQL between dates

本秂侑毒 提交于 2019-12-11 11:43:02

问题


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'


来源:https://stackoverflow.com/questions/9950340/sql-between-dates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!