3

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:


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


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

Michael Currie
  • 12,486
  • 8
  • 41
  • 56
Brian
  • 51
  • 3

3 Answers3

2

cast both timestamps as dates

For SQL Server

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

For Oracle

Select * 
from Batch 
where trunc(openingtime) <> trunc(closingtime)
a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
lookslikeanevo
  • 566
  • 1
  • 5
  • 12
  • what code can I add to so closingtime had to be after 3:00AM? – Brian Jul 13 '15 at 22:46
  • by after 3am would that mean that you want to count stuff milliseconds after 3am? if so then in the where clause add: and datepart(hour, closingtime) >= 3 – lookslikeanevo Jul 15 '15 at 04:34
2

Another way

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

**There will be a closing bracket** @Miyamoto Musashi
Keulraesik
  • 59
  • 5