0

I am currently developing a webpage, and I want to add date&time to my database whenever there is an entry on the database.

  • What meaning would this datatime entry have? When would this entry change if at all? – Oded Feb 18 '12 at 16:11
  • whenever there is an entry to the database, it automatically adds the current date & time when was that certain entry was made and had been successfully added to the database – jade calingin Feb 18 '12 at 16:16

1 Answers1

2

Create a column of type datetime with a default value of getdate()

Each time an insert is added, it should stamp it with the current date/time

SQL code to add the column:

ALTER TABLE {tablename} 
ADD {column_name} {DateTime} {NULL|NOT NULL} DEFAULT {getdate()}
Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
Jeff
  • 1,609
  • 3
  • 20
  • 25
  • can you guide me on how to do that? sorry, cause I'm new to this one :) – jade calingin Feb 18 '12 at 16:14
  • I added to the answer - I am not sure the specifics of your database though. I am assuming MSSQL - The above should work if that is the case. Then all yo would have to do is read from the datetime to see when the row was inserted. – Jeff Feb 18 '12 at 16:17
  • so I just add a column that has a timeStamp data type? then it will automatically generates the date&time for every entry? – jade calingin Feb 18 '12 at 16:18
  • is there any special coding to do in order to get that data type to work? – jade calingin Feb 18 '12 at 16:19
  • I am sorry I edited the answer - I wrote it out wrong. DateTime is the data type, whatever you want for the column name and GetDate() for the default value. – Jeff Feb 18 '12 at 16:23
  • so I'll just add a column for this then it will automatically generates the date&time for every entry on the database? no more coding to do, in order to view the GetDatee() or whatever? – jade calingin Feb 18 '12 at 16:38
  • You will not need anymore coding, no. – Jeff Feb 18 '12 at 16:46
  • 1
    This works for INSERTS if the value is not set to null. It does not work for updates or where the column is included amoung the values (either explicitly by column list or implicitly by insert into select *) – jmoreno Feb 20 '12 at 05:46