我有一个MS SQL 2008数据库表,其列如下:
Date
2012-07-02 18:34:44.487
2012-07-02 18:34:44.480
2012-07-02 18:34:44.193
2012-07-02 18:34:44.193
当我这样做时
SELECT *
FROM table1
WHERE Date BETWEEN '2012-07-02' AND '2012-07-02'
我一无所获。
我可以做些什么来获得上述所有结果,因为它们介于两个日期之间。
答案 0 :(得分:5)
单程
Select * from table1 where Date >= '2012-07-02' and Date < '2012-07-03'
这也比两者更安全,因为你们之间也会获得第三个午夜值,请参阅How Does Between Work With Dates In SQL Server?
答案 1 :(得分:0)
使用dateadd
Select *
from table1
where Date between '2012-07-02' and dateadd(d,1,'2012-07-02')
MSDN文档http://msdn.microsoft.com/en-us/library/ms186819.aspx
粗略地说,原因是SQL将字符串日期(例如此示例)视为给定日期的午夜。所以你真正要问的是between midnight of 2012-07-02 and midnight of 2012-07-03
答案 2 :(得分:0)
试试这个:
Select * from table1 where CONVERT(Date,Date)='2012-07-02'