Milliseconds are truncated when inserting DatTime in Sql Server

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

check the InternalTicks for the date1 and date and see what I mean. What the
hell?

string connString = "Data Source=(local);Initial Catalog=Test;Integrated
Security=True";
SqlCommand command = new SqlCommand("insert into Table_1
(CreateDate) values (@date)");
DateTime date1 = DateTime.Now;
command.Parameters.AddWithValue("@date", date1);

command.Connection = new SqlConnection(connString);

command.Connection.Open();
command.ExecuteNonQuery();

command.CommandText = "Select * from Table_1";
SqlDataReader reader = command.ExecuteReader();
reader.Read();
DateTime date = (DateTime)reader["CreateDate"];
reader.Close();


command.CommandText = "Delete from Table_1";
command.ExecuteNonQuery();
command.Connection.Close();
 
Close! It's 3 millisecond precision.


Jim Hughes said:
SQL Server only stores seconds with 1/3 second precision.

http://vyaskn.tripod.com/searching_date_time_values.htm

eacsub said:
check the InternalTicks for the date1 and date and see what I mean. What
the
hell?

string connString = "Data Source=(local);Initial Catalog=Test;Integrated
Security=True";
SqlCommand command = new SqlCommand("insert into Table_1
(CreateDate) values (@date)");
DateTime date1 = DateTime.Now;
command.Parameters.AddWithValue("@date", date1);

command.Connection = new SqlConnection(connString);

command.Connection.Open();
command.ExecuteNonQuery();

command.CommandText = "Select * from Table_1";
SqlDataReader reader = command.ExecuteReader();
reader.Read();
DateTime date = (DateTime)reader["CreateDate"];
reader.Close();


command.CommandText = "Delete from Table_1";
command.ExecuteNonQuery();
command.Connection.Close();
 
Whoops, you are right of course... Glad I included a link :)

Stephany Young said:
Close! It's 3 millisecond precision.


Jim Hughes said:
SQL Server only stores seconds with 1/3 second precision.

http://vyaskn.tripod.com/searching_date_time_values.htm

eacsub said:
check the InternalTicks for the date1 and date and see what I mean. What
the
hell?

string connString = "Data Source=(local);Initial Catalog=Test;Integrated
Security=True";
SqlCommand command = new SqlCommand("insert into Table_1
(CreateDate) values (@date)");
DateTime date1 = DateTime.Now;
command.Parameters.AddWithValue("@date", date1);

command.Connection = new SqlConnection(connString);

command.Connection.Open();
command.ExecuteNonQuery();

command.CommandText = "Select * from Table_1";
SqlDataReader reader = command.ExecuteReader();
reader.Read();
DateTime date = (DateTime)reader["CreateDate"];
reader.Close();


command.CommandText = "Delete from Table_1";
command.ExecuteNonQuery();
command.Connection.Close();
 
Back
Top