Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime inserted in SqlServer changes value

When I insert these values

09/30/2013 05:04:56.599 
09/30/2013 05:04:56.599
09/30/2013 05:04:56.599
09/30/2013 05:04:57.082

in SqlServer database, the millisecond value changes in a weird way

2013-09-30 05:04:56.600 
2013-09-30 05:04:56.600 
2013-09-30 05:04:56.600 
2013-09-30 05:04:57.083

What's wrong ?

Edit: Relevant code:

        com = new SqlCommand();
        com.Connection = con;
        com.CommandText = @"INSERT INTO [AuthSourceTimings]
                                   ([FileName]
                                   ,[JobID]
                                   ,[JobCreationTime]
                                   ,[JobSendTime]
                                   ,[JobAckTime]
                                   ,[JobDoneTime])
                             VALUES
                                   (@FileName
                                   ,@JobID
                                   ,@JobCreationTime
                                   ,@JobSendTime
                                   ,@JobAckTime
                                   ,@JobDoneTime)
                            ";

        com.Parameters.AddWithValue("@FileName", fileName);
        com.Parameters.AddWithValue("@JobID", t.JobID);

        com.Parameters.AddWithValue("@JobCreationTime", t.JobCreationTime == DateTime.MinValue ? (object)DBNull.Value : (object)t.JobCreationTime);
        com.Parameters.AddWithValue("@JobSendTime", t.JobSendTime == DateTime.MinValue ? (object)DBNull.Value : (object)t.JobSendTime);
        com.Parameters.AddWithValue("@JobAckTime", t.JobAcknowledgementTime == DateTime.MinValue ? (object)DBNull.Value : (object)t.JobAcknowledgementTime);
        com.Parameters.AddWithValue("@JobDoneTime", t.JobCompletionTime == DateTime.MinValue ? (object)DBNull.Value : (object)t.JobCompletionTime);

        com.ExecuteNonQuery();
like image 209
Akshay J Avatar asked Oct 01 '13 11:10

Akshay J


People also ask

How can I insert datetime table value in SQL?

Always use ANSI default string literal format for date i.e. YYYY-MM-DD like below. INSERT INTO EMPLOYEE (EMPID, FULLNAME, DESIGNATION, JOINING, SAL, DEPTNAME) VALUES(8976, 'JOHN', 'JOE', 'ANALYST', '1990-12-12', 30000, 'Analytics'); It will insert your data in RDBMS i.e. MySQL, PostgreSQL, SQL Server.

Does insert () replace?

You can use the INSERT OR REPLACE statement to write new rows or replace existing rows in the table. The syntax and behavior of the INSERT OR REPLACE statement is similar to the INSERT statement. Unlike the INSERT statement, the INSERT OR REPLACE statement does not generate an error if a row already exists.

How can I update a datetime field in SQL?

To update with the current date and time: UPDATE table_name SET date_field = CURRENT_TIMESTAMP; To update with a specific date value: UPDATE table_name SET date_field = 'YYYY-MM-DD HH:MM:SS.


2 Answers

Milliseconds are stored only with precision of about 1/300th of a second in datetime format so that's where the inaccuracy comes from.

You can check this answer: Why is SQL Server losing a millisecond?

To get higher precision (100 nanoseconds), you can use DATETIME2 which was introduced in SQL Server 2008. You can get more information here:

  • http://msdn.microsoft.com/en-us/library/bb677335.aspx
  • http://www.mssqltips.com/sqlservertip/1616/sql-server-2008-date-and-time-data-types/
like image 117
Szymon Avatar answered Sep 30 '22 15:09

Szymon


You are probably using the SQL Server type DATETIME, which the documentation states:

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

The DATETIME2 type should be used if you want full compatibility with .NET DateTime. Both of these have an accuracy of 100 nanoseconds.

There aren't many good reasons to use DATETIME anymore. A DATETIME2(7) takes up 8 bytes which is the same as a DATETIME, but has a far greater accuracy and range. A DATETIME2(3) takes up 6 bytes and still has better accuracy (3 decimals without the weird rounding behavior detailed above).

like image 34
Cory Nelson Avatar answered Sep 30 '22 15:09

Cory Nelson