我有一个 SQL Server 表,其中有一列称为statusdate
ODBC 日期/时间。
我需要创建一个SELECT query WHERE statusdate
可以返回 2 个日期(开始日期和结束日期)之间的范围。我被告知DATEDIFF
在我的WHERE
声明中使用。这是正确的,语法是什么?
问问题
4638 次
2 回答
3
如果您使用的是 SQL Server 2008/2012 和date
您可以使用的数据类型。
select *
from YourTable
where statusdate between @StartDate and @EndDate
如果您使用datetime
,我建议您使用>=
and<
来确保您获得所有值,而不管时间部分如何。
select *
from YourTable
where statusdate >= @StartDate and
statusdate < dateadd(day, 1, @EndDate)
更新
有人告诉我在 WHERE 语句中使用 DATEDIFF。它是否正确
不,您不应该datediff
为此使用。
语法是什么?
在这里...但不要使用它。
select *
from YourTable
where datediff(day, statusdate, @StartDate) = 0 and
datediff(day, statusdate, @EndDate) = 0
于 2012-05-02T18:27:14.847 回答
0
你为什么不做一个BETWEEN
?:
SELECT *
FROM YourTable
WHERE statusdate BETWEEN StartDate AND EndDate
于 2012-05-02T17:58:35.590 回答