The behaviour you have both expected and also documented in SQL Server 2016's breaking changes.
Under database compatibility level 130, implicit conversions from datetime to datetime2 data types show improved accuracy by accounting for the fractional milliseconds, resulting in different converted values. Use explicit casting to datetime2 datatype whenever a mixed comparison scenario between datetime and datetime2 datatypes exists. For more information, see this Microsoft Support Article.
With 2016 Microsoft updated the behaviour for converting from the old datetime
data type to the newer ones to be more accurate. Previously a datetime
value like 2019-09-06 11:28:05.757
converted to the datetime2(7)
value 2019-09-06 11:28:05.7570000
, however, in 2016+ it is now accurately converted to 2019-09-06 11:28:05.7566667
. This is because a datetime
is accurate to 1/300 of a second, not 1/1000, so the conversion before was actually adding time to the value.
For your query, you firstly have the datetimeoffset(7)
value 2019-09-06 11:28:05.7570000
. You then compare that value to the datetime
value 2019-09-06 11:28:05.757
. As the 2 data types are different one is implicitly converted to the other. datetimeoffset
has a higher precendence, so the datetime
value (2019-09-06 11:28:05.757
) is converted to a datetimeoffset
the value 2019-09-06 11:28:05.7566667
, which is less that your other datetimeoffset
value 2019-09-06 11:28:05.7570000
.
As mentioned, in 2014 (and prior) SQL Server inaccurately converted the datetime
value to 2019-09-06 11:28:05.7570000
, which isn't less than itself. This is why the behaviour differs.
If you want the same behaviour, I would suggest (as the breaking change does), using an explicit conversion, and convert to a datetimeoffset(3)
. For example, the below does not return a result set in either instance:
DECLARE @P1 datetimeoffset;
SET @P1 = '2019-09-06 11:28:05.757';
SELECT *
FROM (SELECT cast('2019-09-06 11:28:05.757' as datetime) as test_date) data
WHERE CONVERT(datetimeoffset(3),test_date) < @P1;
2014, 2019 (Linux)