2

I have a SqlCommand which inserts a datetime into column using a SqlParameter. At present I use DateTime.Now as the value of this parameter.

I believe this will add the datetime on the user's PC which is not consistent.

How do I change this so the datetime of the database server is inserted?

EDIT:

I should have made it clear this was just for inserts

5 Answers 5

6

Don't pass in a parameter for the current date; get SQL Server to generate it.

The SQL to do what you described is GETDATE(), though you may want to consider using GETUTCDATE(), as it will behave better with respect to timezone changes (and moving the server!)

See MSDN for more details.

Sign up to request clarification or add additional context in comments.

6 Comments

Can a SQL Server function be specified as the value of a SQLParameter? If not I assume the best method would be to use a default constraint as suggested in another answer
I don't think it can. Default constraint would work well if you're currently inserting directly into the table - I was assuming that this was part of a larger Stored Procedure.
+1: IMHO, this is the best solution. Nonetheless, if DataSets are used, the DateColumn.DateTimeMode may be used to have all users have consistent TimeZone settings (Utc, I assume). See: msdn.microsoft.com/en-us/library/…
@Dave: you may add the DEFAULT CONSTRAINT as marc_s suggested, or you can just call GETDATE() in your SP. In both cases you would not pass the value as the parameter to the SP.
@van: Indeed - a Default Constraint will work in either circumstance. If I already had a sproc I'd probably keep the logic in the sproc and out of the table definition, but each to their own.
|
5

Is this strictly for INSERTs ? Or for updates as well?

If it's for INSERTs only, you could declare a DEFAULT constraint on your DATETIME column on the SQL Server side, and then just not insert a value from the client side - thus using the server's default.

In SQL:

 ALTER TABLE YourTable 
   ADD CONSTRAINT DF_YourTable_YourColumn
     DEFAULT (GETDATE()) FOR YourColumn

and then in your SQL command, just don't mention the "YourColumn" and don't provide a value for it - the server will use getdate() to get the current date/time on the server instead.

Marc

Comments

0

In that case, don't pass a datetime value as a parameter. Instead use the GetDate() function and insert that result in the table

insert into X (Id, Col1, Col2) values (1, 'Customer1', GetDate())

Hope this helps

Comments

0

You may use stored-procedure and GETDATE(),

tableName(no: int, name: varchar(40), date datetime)

Stored procedure

CREATE PROCEDURE  AddRec
@no int,
@name varchar(40)
AS
insert into tableName
 values (@no,@name,getdate())
RETURN

Code to execute stored-procedure

SqlConnection cn=new SqlConnection("Your_Cn_Str");
SqlCommand cmd=new SqlCommand();
cmd.CommandText="AddRec";
cmd.CommandType=CommandType.StoredProcedue;
cmd.Connection=cn;

cmd.Parameters.AddWithValue("@no",10);
cmd.Parameters.AddWithValue("@name","Foo");

cn.Open();
cmd.ExecuteNonQuery();
cn.Close();

Comments

0

http://doc.ddart.net/mssql/sql70/ga-gz_1.htm

1 Comment

This is for SQL 7... and it's still just a copy of Books On Line that was never updated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.