SQL Server default date time stamp?

前端 未结 5 978
轮回少年
轮回少年 2020-12-18 20:55

I have a field that when something is inserted I want it to get the current Date & Time and insert this into the database. Is there a way to get the date & time, and

相关标签:
5条回答
  • 2020-12-18 21:24

    GETDATE() is a date and time in SQL Server.

    Run SELECT GETDATE() to verify this.

    What is the datatype of your field? If it's DATE then it will not hold time values as well.

    0 讨论(0)
  • 2020-12-18 21:30

    Most SQL implementations (engines) do have CURRENT_TIMESTAMP function.

    0 讨论(0)
  • 2020-12-18 21:36

    SYSDATETIME() will get the current date and time.

    Make sure the data type of the column is datetime, and not just date or it won't be able to hold a datetime.

    0 讨论(0)
  • 2020-12-18 21:37

    One easy way is to give the field a default constraint:

    create table YourTable 
        (
        ... other columns ...
        CreateDt datetime default getdate(),
        ... other columns ...
        )
    

    A disadvantage of this method is that you can overwrite the value by specifying it in an insert clause.

    0 讨论(0)
  • 2020-12-18 21:39

    Personally I would like to use GETUTCDATE() instead GETDATE() to avoid confusions.

    0 讨论(0)
提交回复
热议问题