Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert current date into a date column using T-SQL?

Tags:

I'm trying to insert a date when a user decides to deactivate or activate an UserID. I'm trying to use a SP to trigger that but apparantly this is harder than I thought it would be.

I can do a

SELECT GETDATE()

to get the date but is there a way to insert that information from GETDATE and put it into a column I want?

like image 792
nhat Avatar asked Nov 02 '11 14:11

nhat


2 Answers

Couple of ways. Firstly, if you're adding a row each time a [de]activation occurs, you can set the column default to GETDATE() and not set the value in the insert. Otherwise,

UPDATE TableName SET [ColumnName] = GETDATE() WHERE UserId = @userId 
like image 107
harriyott Avatar answered Oct 05 '22 20:10

harriyott


To insert a new row into a given table (tblTable) :

INSERT INTO tblTable (DateColumn) VALUES (GETDATE())

To update an existing row :

UPDATE tblTable SET DateColumn = GETDATE()
 WHERE ID = RequiredUpdateID

Note that when INSERTing a new row you will need to observe any constraints which are on the table - most likely the NOT NULL constraint - so you may need to provide values for other columns eg...

 INSERT INTO tblTable (Name, Type, DateColumn) VALUES ('John', 7, GETDATE())
like image 36
El Ronnoco Avatar answered Oct 05 '22 19:10

El Ronnoco