Solved

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

Posted on 2014-11-07
5
99 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 33

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 33

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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
monitoring configuration for SQL server DB 32 47
Table where row act as column 11 69
SQL Server Trigger 8 37
SQL 2012 Instance Problem 3 55
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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

756 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