I'm a beginner to SQL Server 2005.
I'm building a small form with a SQL2005 database. I'm creating the database and adding a field called DateInsert. When the user clicks the submit button and the form data gets written to my database, how do I automatically generate a timestamp and write it to my DateInsert Field in the database?
Thanks in advance!
Hello,
You can use GetDate() in your insert statement. Such as:
INSERT INTO MyTableName
(ID,FIELD1,FIELD2,TIMESTAMP) VALUES (1,"Example","Example", GetDate())
Hope this helps.
Thanks for your help. That is what I needed.
Is there a way to bind this to the FormView Insert Item on the formview control? I'm looking for the autogenerated code for the control and can't seem to find it.
||| You can avoid the extra work and just take care of it on the database-end like described above.
Or you can use the getdate() as the default value for that column. This can be done in enterprise manager.
|||You can use default constraint with the field if you are sure you want the current date time value to appear in the field on every insert. Below is the syntax you can use at the time of creating a table.
create table <table name>( <column name>datetime default (getdate() ), )
If you've already created the table then below is the alter query you can use to set the default value constraint.
alter table <table name>add constraint [df_<table name>_name>]default (getdate() )forname>
If you've added the default constraint for any column using one of above queries then you don't need to include that column in the insert list,
the default value will automatically inserted for that column.
The way is provide getdate() as the value for that field in the Insert query.
Hope this will help.
No comments:
Post a Comment