Insert a datetime value with GetDate() function to a SQL server (2005) table?

Posted by David.Chu.ca on Stack Overflow See other posts from Stack Overflow or by David.Chu.ca
Published on 2010-06-10T21:22:07Z Indexed on 2010/06/10 21:32 UTC
Read the original article Hit count: 316

Filed under:
|

I am working (or fixing bugs) on an application which was developed in VS 2005 C#. The application saves data to a SQL server 2005. One of insert SQL statement tries to insert a time-stamp value to a field with GetDate() TSQL function as date time value.

Insert into table1 (field1, ... fieldDt) values ('value1', ... GetDate());

The reason to use GetDate() function is that the SQL server may be at a remove site, and the date time may be in a difference time zone. Therefore, GetDate() will always get a date from the server. As the function can be verified in SQL Management Studio, this is what I get:

SELECT GetDate(), LEN(GetDate());
-- 2010-06-10 14:04:48.293   19

One thing I realize is that the length is not up to the milliseconds, i.e., 19 is actually for '2010-06-10 14:04:48'. Anyway, the issue I have right now is that after the insert, the fieldDt actually has a date time value up to minutes, for example, '2010-06-10 14:04:00'. I am not sure why. I don't have permission to update or change the table with a trigger to update the field.

My question is that how I can use a INSERT T-SQL to add a new row with a date time value ( SQL server's local date time) with a precision up to milliseconds?

© Stack Overflow or respective owner

Related posts about visual-studio

Related posts about tsql