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();
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.
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.
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.
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:
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With