Friday, February 24, 2012

Add date to record in SQL server each time record is added

Hi

Can anyone advise me as to how I can add the date and time to 2 columns in the sql server database for each record that is added. I'd prefer not to use the webform. Can sql server add the date automatically to the row?

thanks

Sure, you can do it a number of ways. Add a datetime field to your table, and set it's default value to either GetDate() or GetUTCDate(). That's one approach, but it only works for inserts.

If you want to have it updated on every update, then you will need to implement that in a trigger, or modify your UPDATE statements to set the field to GetDate() or GetUTCDate().

|||

Cool, I'll try that.

I want it INSERTED once for every record so i think the first way is the way to go.

Tell me this: do i need to add any code to the web form or is it enough to add the field to the db table and set the values?

Thanks!!

|||if you add a datetime column to a table, and set it's default value to getdate(), then whenever you insert a row into that table, the new column will have the date and time that the row was inserted. You don't have to change any SQL Statements at all, unless you've specifically created a dynamic update statement that will now attempt to insert a value into the new column, or something similiar. In most cases (I'll guess 95%+) nothing needs be changed at all.|||Cool. That is just perfect. No change needed at all. Thanks for your help!!|||I tried using the getdate() as the default; however, the date always comes out to be 1/1/1900. Any advise?|||

Haywire:

I tried using the getdate() as the default; however, the date always comes out to be 1/1/1900. Any advise?


What's the data type of the column? It should be be datetime.

No comments:

Post a Comment