Reputation:
How do I insert a current_timestamp into an SQL Server 2005 database datable with a timestamp column?
It should be simple but I cannot get it to work. Examples would be much appreciated.
Upvotes: 8
Views: 82426
Reputation: 5585
if you can execute a query from PHP then it should just be a matter of using 'getdate()' ;
update MyTable set MyColumn=getdate();
or
insert into MyTable (MyColumn) values (getdate());
Upvotes: 9
Reputation: 2240
I realize this is an old post but I ran across this and figured others might too.
What I do is to create a field in the table using the datatype of datetime and then for the column properties for the default Value or binding, I enter getdate()
. Every record that gets entered into the table now has a date and time stamp.
Upvotes: 0
Reputation: 1099
To insert data into a timeStamp field, I think you have to use DEFAULT
.
For example:
INSERT INTO User(username, EnterTS) VALUES ('user123', DEFAULT)
where EnterTS
is a TimeStamp field
Upvotes: 3
Reputation: 96552
If you explain the error you are receiving (and post your insert or update code), it might make it easier to see what your problem is. One possible issue is that you must be inserting into a datetime field (or in 2008 you could use a date field as well). Occasionally people think that they can insert the current date into a timestamp field but even though the name of this data type seems as if it should be date related, this data type actaully has nothing to do with dates and times and cannot accept date data.
Upvotes: 0
Reputation: 57815
The column datatype should be datetime
You can either get the database to work out the date:
$sql = 'INSERT INTO tablename (fieldname) VALUES (getdate())';
or get PHP to work out the date
$sql = 'INSERT INTO tablename (fieldname) VALUES (\'' . date('Y-m-d H:i:s') . '\')';
then something like mssql_execute($sql);
but this depends on how you are connecting to your database
Upvotes: 6
Reputation: 340211
You just use the normal mechanism to execute your queries into SQL Server, and use what follows.
The current_timestamp function returns it
insert into table (creation_date) VALUES (current_timestamp)
Complete example
CREATE table timestamp_test (creation_timestamp datetime)
INSERT INTO timestamp_test (creation_timestamp) VALUES (current_timestamp)
SELECT * FROM timestamp_test
DROP TABLE timestamp_test
Upvotes: 2