Amit Kumar
Amit Kumar

Reputation: 5962

how to add today date as default value in sql server

in sql server. my database name RequestInfo, I have a table name RequestedDate, which have data type is datetime. now i want when ever the value of other columns of table is inserted, then automatically in the RequestedDate column, today date should inserted. i am using this query but it shows no today in built function.

alter table RequestInfo add default Today() for RequestedDate

Upvotes: 0

Views: 12904

Answers (2)

JavaGeek
JavaGeek

Reputation: 581

This worked for me:

ALTER TABLE YOUR_TABLE ADD Date_Created TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

Upvotes: 1

M.Ali
M.Ali

Reputation: 69514

Define Default Value When Creating Table

CREATE TABLE TestTable 
(
ID INT PRIMARY KEY NOT NULL,
DATECOLUMN DATETIME DEFAULT GETDATE() --<-- Default Value
)

Already Existing Table on already Existing Column

ALTER TABLE TestTable 
ADD CONSTRAINT DF_YourTable DEFAULT GETDATE() FOR DATECOLUMN

Add a new Column to an Existing Table With Default Value

ALTER TABLE TestTable
ADD New_DATE_COLUMN DATETIME DEFAULT GETDATE()

Upvotes: 4

Related Questions