SQL:如何在忽略时间的情况下比较日期?

时间:2015-07-13 22:25:38

标签: sql

如何在忽略时间的情况下比较时间戳的日期?

我只想比较月/日/年。例如:

select * from Batch
where openingtime <> closingtime

问题是,这将显示太多批次,因为它将包括OpeningTimeClosingTime仅在一天中的时间不同的批次:

OpeningTime = 2010-07-07 11:19:29.000

ClosingTime = 2010-07-07 19:19:22.000

3 个答案:

答案 0 :(得分:2)

将两个时间戳都转换为日期

对于SQL Server

Select * 
from Batch 
where cast(openingtime as date) <> cast(closingtime as date)

对于Oracle

Select * 
from Batch 
where trunc(openingtime) <> trunc(closingtime)

答案 1 :(得分:2)

另一种方式

Select * from Batch where      
CONVERT(VARCHAR(10),openingtime,110<>CONVERT(VARCHAR(10),closingtime,110)

答案 2 :(得分:2)

Select * from Batch where      
CONVERT(VARCHAR(10),openingtime,110)<>CONVERT(VARCHAR(10),closingtime,110)

**There will be a closing bracket** @Miyamoto Musashi