我正在尝试在下面的where子句中给出日期范围..但是它没有给出正确的o / p
declare @StartDate DATETIME, @EndDate DATETIME
set @StartDate='9/01/2011'
set @EndDate='1/30/2012'
Select * from mytable Where MONTH(WT.ToDate) >= MONTH(@StartDate) AND MONTH(WT.ToDate) <=MONTH(@Enddate)
AND YEAR(WT.ToDate)>= YEAR(@StartDate) AND YEAR(WT.ToDate) <=YEAR(@Enddate)
请帮忙
答案 0 :(得分:0)
什么是WT.ToDate,WT指的是什么?
应该是。
declare @StartDate DATETIME, @EndDate DATETIME
set @StartDate='9/01/2011'
set @EndDate='1/30/2012'
Select * from mytable as WT Where MONTH(WT.ToDate) >= MONTH(@StartDate) AND MONTH(WT.ToDate) <=MONTH(@Enddate)
AND YEAR(WT.ToDate)>= YEAR(@StartDate) AND YEAR(WT.ToDate) <=YEAR(@Enddate)
答案 1 :(得分:0)
首先,请注意我已经更改了您指定日期的格式,以避免任何误解的风险(现在为yyyyMMdd)。
其次,尝试这样一个条款:
declare @StartDate DATETIME, @EndDate DATETIME
set @StartDate='20110901'
set @EndDate='20120130'
Select *
from mytable
Where WT.ToDate >= @StartDate AND WT.ToDate < @EndDate
注意,这不会返回2012年1月30日的行,所以只需根据需要调整@EndDate