Does anyone know of a function as such that I can use to add an automatic date and timestamp in a column for when a user adds a record to the database table?
This question is related to
sql
sql-server
sql-server-2008
sql-server-2005
You can make a default constraint on this column that will put a default getdate() as a value.
Example:
alter table dbo.TABLE
add constraint df_TABLE_DATE default getdate() for DATE_COLUMN
You can use a datetime
field and set it's default value to GetDate()
.
CREATE TABLE [dbo].[Test](
[TimeStamp] [datetime] NOT NULL CONSTRAINT [DF_Test_TimeStamp] DEFAULT (GetDate()),
[Foo] [varchar](50) NOT NULL
) ON [PRIMARY]
you can use DateAdd on a trigger or a computed column if the timestamp you are adding is fixed or dependent of another column
You can pass GetDate()
function as an parameter to your insert query
e.g
Insert into table (col1,CreatedOn) values (value1,Getdate())
Source: Stackoverflow.com