Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round .NET DateTime milliseconds, so it can fit SQL Server milliseconds

I want to convert the datetime value to the value that I will get from SQL Server 2008.

SQL Server truncate the milliseconds to 3 digits, so I truncate the milliseconds already. But the problem is that as you can see here: Milliseconds wrong when converting from XML to SQL Server datetime. SQL Server also has an precision issue.

like image 264
stacker Avatar asked May 20 '10 09:05

stacker


People also ask

Does SQL DateTime store milliseconds?

Millisecond / microsecond precision optional since version 5.3 . SQLite: Datetimes can be stored as strings with arbitrary precision, but "only the first three digits are significant". Oracle: Default 1 microsecond. Optional precision to 1 nanosecond.

How do I get millisecond in SQL?

We can use DATEPART() function to get the MILLISECOND part of the DateTime in Sql Server, here we need to specify datepart parameter of the DATEPART function as millisecond or mi .


1 Answers

Here's what you want:

using System.Data.SqlTypes; // from System.Data.dll

public static DateTime RoundToSqlDateTime(DateTime date)
{
  return new SqlDateTime(date).Value;
}
like image 92
RobSiklos Avatar answered Oct 04 '22 07:10

RobSiklos