解决方法
您应该查看sql Server中可用的日期时间格式:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
yyyy-mm-dd hh:mi是你应该使用的:
尝试:
SELECT * FROM Records WHERE DateCreated>='2007-02-30 10:32' AND DateCreated<='2008-06-21 14:19'
在上述查询中,如果DateCreated是datetime列,则字符串将被转换为datetime数据类型.并且查询将工作.
您可以创建datetime数据类型的局部变量,并使用以下查询:
DECLARE @StartDate datetime,@EndDate datetime SELECT @StartDate='2007-02-30 10:32',@EndDate='2008-06-21 14:19' SELECT * FROM Records WHERE DateCreated>=@StartDate AND DateCreated<=@EndDate
我喜欢使用< =,> =,或>因为它允许比BETWEEN更多的灵活性,并强制您考虑包括端点.
另一件需要考虑的事情是从一整天开始获取所有数据:
DECLARE @StartDate datetime,@EndDate datetime --set the days you want SELECT @StartDate='2007-02-30 10:32',@EndDate='2008-06-21 14:19' --remove the time SELECT @StartDate=DATEADD(day,DATEDIFF(day,@StartDate),0),@EndDate=DATEADD(day,@EndDate),0) --get everything on '2007-02-30' up to the end of the day on '2008-06-21' SELECT * FROM Records WHERE DateCreated>=@StartDate AND DateCreated<@EndDate+1