Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Selected datetime value doesn't match the original value on SQL Server 2016 #680
Driver version or jar name
SQL Server version
SQL Server 2016
Client operating system
Windows 7 Enterprise (64bit)
java version "1.7.0_79"
CREATE TABLE DATETIMETEST (
INSERT INTO DATETIMETEST VALUES(1, '2018-04-01 12:00:00.000');
On SQL Server 2016, selected datetime value doesn't match the original value.
But the problem doesn't occur in .NET.
Expected behavior and actual behavior
Expected: it matches.
You are correct that the issue is caused by the server-side change you noted. The reason the change breaks things is because the Timestamp data type in JDBC maps to the datetime2 data type in SQL Server. So in the first query, when i = 2, the database returns 2018-04-01 12:00:01.003 which is stored into date1 which becomes the Timestamp 2018-04-01 12:00:01.003000. When you send that Timestamp as a parameter in query 2, the 2016 database sees a comparison between the datetime2 parameter value of 2018-04-01 12:00:01.003000 and the database datetime value of 2018-04-01 12:00:01.003333 which it equates to not equal. (Due to the data handling accuracy "improvements" in 2016, datetime values are made more "accurate" when converting to datetime2 since datetime is actually granular to 1/300 of a second.)
You can see this in practice on the server side by simply running:
The reason this is not an issue in .NET is .NET has native DateTime and DateTime2 data types whereas JDBC only has Timestamp.
The most reasonable workarounds are to run the database in SQL Server 2014 compatibility mode or convert all datetime columns to datetime2(3).