Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Friday, August 18, 2017

Don't Let Microsoft SQL Round Your DateTime Field's Milliseconds

I needed to export some data out of Application Insights and save it locally.  While doing a sanity check, I realized that some of my datetime values were not the same.

Therefore, I created a simple test.
  1. Create a table with just a datetime field.
  2. Insert the value I expected.
  3. Select that data back out and witness the rounded value.



I finally found an answer here:
https://stackoverflow.com/questions/36414315/inserting-datetime-with-milliseconds-into-sql-server-table-issue

MS SQL rounds the DateTime field in increments of .000, .003, and .007.  I would assume that means it is saving on memory space behind the scenes by only offering 3 options for the single digit of milliseconds?

Whatever the case, if precision is necessary (as it was for the Application Insights data I was getting),  use the newer datetime2(3) field definition.