Use current date as default value for column

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

Example:

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

Line:

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

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

+63
sql sql-server
Apr 19 2018-11-11T00:
source share
8 answers

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

 ALTER TABLE myTable ADD CONSTRAINT CONSTRAINT_NAME DEFAULT GETDATE() FOR myColumn 
+90
Apr 19 2018-11-11T00:
source share
 CREATE TABLE Orders( O_Id int NOT NULL, OrderNo int NOT NULL, P_Id int, OrderDate date DEFAULT GETDATE() // you can set default constraints while creating the table ) 
+17
Apr 19 2018-11-11T00:
source share

You can use:

 Insert into Event(Description,Date) values('teste', GETDATE()); 

In addition, you can modify the table so that "Date" has a default value of "GETDATE ()"

+3
Apr 19 2018-11-11T00:
source share

Select the column name of the table where you want to get the default value of the current date

  ALTER TABLE [dbo].[Table_Name] ADD CONSTRAINT [Constraint_Name] DEFAULT (getdate()) FOR [Column_Name] 

Change table query

 Alter TABLE [dbo].[Table_Name]( [PDate] [datetime] Default GetDate()) 
+2
Dec 31 '15 at 16:12
source share

To use the current date as the default for the date column, you need to:

1 - constructor of open tables

2 - select a column

3 - go to column properties

4 - set the default value or binding propriete To ( getdate () )

enter image description here

0
Dec 10 '17 at 12:51 on
source share

The syntax for creating a table could be:

 Create table api_key(api_key_id INT NOT NULL IDENTITY(1,1) PRIMARY KEY, date_added date DEFAULT GetDate()); 

The syntax for an insert request can be:

 Insert into api_key values(GETDATE()); 
0
Apr 05 '18 at 22:11
source share

I also encountered this need for my database project. I decided to share my findings here.

1) There is no path to the NOT NULL field without a default value when the data already exists ( Can I add a non-zero column without a DEFAULT value )

2) This topic has long been addressed. Here is the 2008 question ( adding a default column to an existing table in SQL Server )

3) The DEFAULT constraint is used to provide a default value for a column. A default value will be added to all new entries unless a different value is specified. ( https://www.w3schools.com/sql/sql_default.asp )

4) The Visual Studio database project that I use for development is really good for creating change scripts for you. This is a change script created to promote my database:

 GO PRINT N'Altering [dbo].[PROD_WHSE_ACTUAL]...'; GO ALTER TABLE [dbo].[PROD_WHSE_ACTUAL] ADD [DATE] DATE DEFAULT getdate() NOT NULL; 

-

Here are the steps I took to upgrade my database using Visual Studio for development.

1) Add a default value (Visual Studio SSDT: DB project: table designer) enter image description here

2) Use the schema comparison tool to generate a change scenario.

 code already provided above 

3) Review the data BEFORE applying the changes. enter image description here

4) View data AFTER changes are applied. enter image description here

0
Jul 31 '18 at 20:15
source share

Right-click the table and select Design, then click the column for which you want to set the default value.

Then at the bottom of the page, in the column properties, set the default value or binding to: 'getdate ()'

0
Jul 08 '19 at 11:12
source share



All Articles