简体   繁体   English

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

[英]SQL: How to compare dates while ignoring the time?

How can I compare just the dates of timestamps while ignoring the times? 如何比较时间戳的日期而忽略时间?

I just want to compare the month/date/year. 我只想比较月/日/年。 For example: 例如:

select * from Batch
where openingtime <> closingtime

The problem is that this will show too many batches, since it will include batches where OpeningTime and ClosingTime differ only in the time of day: 问题是这将显示太多批次,因为它将包括OpeningTimeClosingTime仅在一天中的时间不同的批次:


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


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

cast both timestamps as dates 将两个时间戳都作为日期

For SQL Server 对于SQL Server

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

For Oracle 对于Oracle

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

Another way 其他方式

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM