Insert Date SQL Database example

Add Datetime Default Value SQL

To create a table with default date time value, you will have to use getdate() as column default value. Be-careful while putting default value that column should not be not null by default. Please have sample create table script where I am keeping primary key incremental by default, column with varchar including date and datetime column.

create table dbo.Test (
	test_id int identity not null,
	smID varchar (20) null,
	eventDate date null,
	createTime datetime default getdate() null,
	updateTime datetime null,
	primary key (test_id) 
)
  • Sample insert statement to insert data to the table:
INSERT INTO dbo.Test(
  smID
  ,eventDate
  ,updateTime
) VALUES (
   '123654' -- smID - IN varchar(20)
   ,'20151226' -- eventDate - IN date
   ,'12/26/2015 12:00:00 AM' -- updateTime - IN datetime
)
  • I have inserted sample data which shows below:

Add Datetime Default Value SQL

Reference:

Leave a Reply

Your email address will not be published. Required fields are marked *