SQL cRetae table with Date Field that defaults to today's date


What SQL syntax would I use to create a table in SQL that has a Date Field that defaults to today's date when data is uploaded?
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
      DateColumnWithDefault DATE NOT NULL
                                 CONSTRAINT DF_YourTable_DateColumnWithDefault DEFAULT ( GETDATE() ) ,
      Payload NVARCHAR(255)

INSERT  INTO dbo.YourTable
        ( Payload )
VALUES  ( N'!' );

FROM    dbo.YourTable YT;

DROP TABLE dbo.YourTable;

Open in new window

Scott PletcherSenior DBACommented:
The simplest syntax is just to add "DEFAULT GETDATE()" after the data type of the column:

CREATE TABLE tablename (
    id int,
    date_column datetime DEFAULT GETDATE(),

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
ste5anSenior DeveloperCommented:
Please use always explicitly named constraints. Otherwise you'll have a hard time reading error messages. While this is not really a DEFAULT problem, it especially is created by the short

CREATE TABLE tablename

Open in new window

So, for symmetry or completeness reasons: Also name DEFAULT constraints.
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.