Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use current date as default value for a column

Tags:

sql

sql-server

Is there a way to set the default value of a column to DateTime.Now in Sql Server?

Example:

table Event Id int (auto-increment) not null Description nvarchar(50) not null Date datetime not null 

The line:

Insert into Event(Description) values('teste'); 

should insert a row and the Date value should be the current date.

like image 358
dcarneiro Avatar asked Apr 19 '11 14:04

dcarneiro


People also ask

How do I get the default date in SQL?

The default date format is determined by the current language setting. You can change the date format by using the SET LANGUAGE and SET DATEFORMAT statements. The ydm format isn't supported for date.


1 Answers

Add a default constraint with the GETDATE() function as value.

ALTER TABLE myTable    ADD CONSTRAINT CONSTRAINT_NAME     DEFAULT GETDATE() FOR myColumn 
like image 77
Jason Avatar answered Sep 30 '22 23:09

Jason