Milliseconds are truncated when inserting DatTime in Sql Server

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();
 
S

Stephany Young

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();
 
J

Jim Hughes

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();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top