Solved

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

Posted on 2014-11-07
5
100 Views
Last Modified: 2014-11-08
Hi

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?
0
Comment
Question by:murbro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 34

Assisted Solution

by:ste5an
ste5an earned 250 total points
ID: 40428621
CREATE TABLE YourTable
    (
      DateColumnWithDefault DATE NOT NULL
                                 CONSTRAINT DF_YourTable_DateColumnWithDefault DEFAULT ( GETDATE() ) ,
      Payload NVARCHAR(255)
    );

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

SELECT  *
FROM    dbo.YourTable YT;

DROP TABLE dbo.YourTable;

Open in new window

0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 40428757
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(),
    ...
    )
0
 

Author Closing Comment

by:murbro
ID: 40430090
thanks
0
 
LVL 34

Expert Comment

by:ste5an
ID: 40430107
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
(
    id INT PRIMARY KEY
);

Open in new window


So, for symmetry or completeness reasons: Also name DEFAULT constraints.
0
 

Author Comment

by:murbro
ID: 40430127
Thanks
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question