Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-11-07
5
Medium Priority
?
105 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:Murray Brown
  • 2
  • 2
5 Comments
 
LVL 36

Assisted Solution

by:ste5an
ste5an earned 1000 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 70

Accepted Solution

by:
Scott Pletcher earned 1000 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:Murray Brown
ID: 40430090
thanks
0
 
LVL 36

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:Murray Brown
ID: 40430127
Thanks
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

926 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