views:

109

answers:

4

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?

A: 

Maybe this would work instead of getdate - SYSDATETIME() look here if you can find what you need - http://msdn.microsoft.com/en-us/library/ms188383.aspx

Misnomer
I think the function is only available in SQL 2008.
David.Chu.ca
Question states SQL Server 2005, but SYSDATETIME() is only available with SQL Server 2008 and up.
G Mastros
oops ya didnt realise it...G Mastros is probably right..check you datatype for that column.
Misnomer
+1  A: 

Check your table. My guess is that the FieldDT column has a data type of SmallDateTime which stores date and time, but with a precision to the nearest minute. If my guess is correct, you will not be able to store seconds or milliseconds unless you change the data type of the column.

G Mastros
You are right, G Mastros. I have to ask DBA to make a change and I have no way to make date time value up to millisecond precision wth SmallDateTime data type. Thanks!
David.Chu.ca
Since the SmallDate has the limitation, I have to use a count of FieldDt with a WHERE clause as a condition to find out FieldDt having a new value or not. I may need to make a request to change db, but it would take a long time at my work company.
David.Chu.ca
A: 

I would guess that you are not storing the GetDate() value in a DateTime field. If you store the value in a datetime field you will get the maximum precision allowed by the DateTime type. Additionally, DateTime is a binary type (a double actually) so 19 means 19 bytes, not 19 characters.

Try to create a simple table with a Datetime field like this

CREATE TABLE [dbo].[DateTable](
[DateField] [datetime] NOT NULL
)

And add a date with

insert into datetable (datefield) values(getdate())

When you execute a select you will get back a value including milliseconds. The following query

select *  from datetable

returns

2010-06-11 00:38:46.660

Panagiotis Kanavos
A: 

As you're on SQL 2005, don't forget the getutcdate() function to ensure that, regardless of where your servers are actually located, you have a constant time reference.

Imagine, you have the server in the UK in winter (i.e. GMT+0), and save a record at 10:30am. You then cut over to a SQL server hosted in California (GMT+8) and 8 hours later save another record.

Using getdate(), both saves record the same time "10:30:00". Using getutcdate(), the first save records at "10:30:00", the second save records "18:30:00".

Not really answering the question, but important in your circumstances.

Neil Moss
Thanks for you comments. getutcdate() is for a date time regardless of the location. However, for my case, the client requires to the local date time to the field, so I have to use GetDate() in TSQL to execute the INSERT SQL on the server side.
David.Chu.ca