How to test two datetimes for equality in T-SQL (i

2019-08-01 05:36发布

问题:

How can I test two datetimes (not including their time components) for equality?

回答1:

Your best bet would be to use DATEDIFF

For example to only compare the months:

SELECT DATEDIFF(month, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');

This is the best way to do comparisons and determine the differences based on your exact need for the query your doing. It even goes down to milisecond.



回答2:

DATEDIFF(day, rem.DueDate , GETDATE()) = 0 


回答3:

To test if the two dates are equal, ignoring the time component:

SELECT DATEDIFF(day, @first, @second) = 0

To test if the two times are equal, ignoring the date component:

SELECT DATEADD(day, -DATEDIFF(day, 0, @first), @first) =
    DATEADD(day, -DATEDIFF(day, 0, @second), @second)


回答4:

This should work:

SELECT * FROM MyTable 
WHERE floor(convert(real,date1))=floor(convert(real,date2))