use current date as default value for a column

SqlSql Server

Sql Problem Overview


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.

Sql Solutions


Solution 1 - Sql

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

ALTER TABLE myTable 
  ADD CONSTRAINT CONSTRAINT_NAME
    DEFAULT GETDATE() FOR myColumn

Solution 2 - Sql

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
)

Solution 3 - Sql

You can use:

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

Also, you can change your table so that 'Date' has a default, "GETDATE()"

Solution 4 - Sql

Select Table Column Name where you want to get default value of Current date

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

Alter Table Query

Alter TABLE [dbo].[Table_Name](
	[PDate] [datetime] Default GetDate())

Solution 5 - Sql

I have also come across this need for my database project. I decided to share my findings here.

  1. There is no way to a NOT NULL field without a default when data already exists (https://stackoverflow.com/questions/3997966/can-i-add-a-not-null-column-without-default-value)

  2. This topic has been addressed for a long time. Here is a 2008 question (https://stackoverflow.com/questions/92082/add-a-column-with-a-default-value-to-an-existing-table-in-sql-server)

  3. The DEFAULT constraint is used to provide a default value for a column. The default value will be added to all new records IF no other 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 about generating change scripts for you. This is the change script created for my DB promotion:

    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 update my database using Visual Studio for development.

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

  2. Use the Schema Comparison tool to generate the change script.

    code already provided above

  3. View the data BEFORE applying the change. enter image description here

  4. View the data AFTER applying the change. enter image description here

Solution 6 - Sql

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

1- open table designer

2- select the column

3- go to column proprerties

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

enter image description here

Solution 7 - Sql

Solution that seems to me much easier to remember :

alter table table_name add column date datetime default current_timestamp 

Solution 8 - Sql

Table creation Syntax can be like:

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

Insertion query syntax can be like:

Insert into api_key values(GETDATE());

Solution 9 - Sql

Right click on the table and click on Design,then click on column that you want to set default value.

Then in bottom of page in column properties set Default value or binding to : 'getdate()'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondcarneiroView Question on Stackoverflow
Solution 1 - SqlJasonView Answer on Stackoverflow
Solution 2 - SqlMayureshView Answer on Stackoverflow
Solution 3 - SqlBlazesView Answer on Stackoverflow
Solution 4 - SqlKhalidView Answer on Stackoverflow
Solution 5 - SqlSherlockSpreadsheetsView Answer on Stackoverflow
Solution 6 - SqlMaher Ben IssaView Answer on Stackoverflow
Solution 7 - Sqlraphael.oesterView Answer on Stackoverflow
Solution 8 - SqltRuEsAtMView Answer on Stackoverflow
Solution 9 - SqlMohammad Reza ShahrestaniView Answer on Stackoverflow