Incorrect comparison of datetime and datetime2
15
votes
2
answers
3272
views
I know that it is not a good practice to have an implicit type convert. But this is really unexpected behavior when a lower value can suddenly become higher.
declare @LastSelectedDate DATETIME = '2021-11-09 13:52:29.187'
declare @LastSelectedDate_1 DATETIME2(7) = '2021-11-09 13:52:29.1866667'
SELECT IIF(@LastSelectedDate_1 > CAST(@LastSelectedDate AS DATETIME2), 1, 0)
SELECT IIF(@LastSelectedDate_1 > @LastSelectedDate, 1, 0)
Is this a bug or I am missing something? I am using sql server 2016.
Asked by Artashes Khachatryan
(1533 rep)
Nov 9, 2021, 03:59 PM
Last activity: Nov 20, 2024, 12:33 PM
Last activity: Nov 20, 2024, 12:33 PM