【问题标题】:SQL: How to compare dates while ignoring the time?SQL:如何在忽略时间的情况下比较日期?
【发布时间】:2015-07-13 22:25:37
【问题描述】:

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

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

select * from Batch
where openingtime <> closingtime

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


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


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

【问题讨论】:

标签: sql


【解决方案1】:

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

对于 SQL Server

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

对于甲骨文

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

【讨论】:

  • 我可以添加什么代码,所以关闭时间必须在凌晨 3:00 之后?
  • 凌晨 3 点之后是否意味着您要计算凌晨 3 点之后的毫秒数?如果是这样,那么在 where 子句中添加:和 datepart(hour, closingtime) >= 3
【解决方案2】:

另一种方式

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

【讨论】:

    【解决方案3】:
    Select * from Batch where      
    CONVERT(VARCHAR(10),openingtime,110)<>CONVERT(VARCHAR(10),closingtime,110)
    
    **There will be a closing bracket** @Miyamoto Musashi
    

    【讨论】:

      猜你喜欢
      • 2021-09-16
      • 2016-02-14
      • 1970-01-01
      • 2015-04-20
      • 2016-11-11
      • 2011-10-25
      • 2012-05-08
      • 1970-01-01
      相关资源
      最近更新 更多