Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Milliseconds value are not getting saved in SQL Server DateTime column

I need to store a DateTime value which is sent to the Database from a C# application which is using DateTime.UtcNow. If I save it to the DateTime column, the milliseconds value are always 000. But while debugging from the application, the milliseconds value is sent from the application to the database. What am I missing?

like image 760
Red Devil Avatar asked Mar 05 '26 11:03

Red Devil


2 Answers

It might happen because datetime column rounds milliseconds part. From documentation;

Accuracy

Rounded to increments of .000, .003, or .007 seconds

Since you didn't show us how often you store your UtcNow value (I assume you store it as a DateTime, not it's string representation because parameterless ToString and other standard formats does not generates millisecond part usually), this may happen but if you do it in short time intervals, it would be really weird always rounding to 000 as milliseconds part. But of course, we can't know that, yet.

On the other hand, datetime2 type does not do any rounding. And it's accuracy is 100 nanoseconds.

Accuracy

100 nanoseconds

like image 160
Soner Gönül Avatar answered Mar 08 '26 00:03

Soner Gönül


Here's no pleasant way to use Datetime because

SQL Server only stores time to approximately 1/300th of a second. These always fall on the 0, 3 and 7 milliseconds

SQL Server 2008 has much more precision available. The datetime2 datatype will accurately store values like this: 2008-12-19 09:31:38.5670514

See reference documentation

like image 35
vahid kargar Avatar answered Mar 08 '26 01:03

vahid kargar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!