none
Time autosave RRS feed

  • Question

  • Hi, How can I save time and date automatically when creating data in a database.And then I can use it in searches and ... Help me 
    • Edited by Ali_Lonbar Saturday, June 22, 2019 5:19 AM
    Saturday, June 22, 2019 4:29 AM

All replies

  • We cab help you if you help us to help you. Your question is very vague and can be interpreted in a multitude of ways.

    So please tell us more about your system and what your exact requirements are.


    Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

    Saturday, June 22, 2019 8:23 AM
  • I want to add the time and date of creating the data when added to the database
    Saturday, June 22, 2019 12:02 PM
  • That is still too terse. Any table in the database? A specific table? What is your database about?


    Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

    Saturday, June 22, 2019 5:52 PM
  • Hi, How can I save time and date automatically when creating data in a database.And then I can use it in searches and ... Help me 

    As Erland says, you do not give us much to go on. However, I'll give it a shot. My assumption is that you want to create one or more tables, and you want to automatically capture the time when a row was inserted? In that case you can use a table creation statement like so:

    CREATE TABLE dbo.MyTable
    (
      RowID bigint identity primary key,
      Col1 int,
      Col2 nvarchar(256),
      DateInserted datetime DEFAULT GETDATE()
    )
     

    The interesting part of the table creation code above is the datetime column, where you use the DEFAULT statement, and indicates what the default value should be - in this case the GETDATE() function. The it is "wash, rinse, repeat", for other new tables.

    If you want to do this for existing tables you use the ALTER TABLE syntax.

    Hope this helps!

    Niels


    http://www.nielsberglund.com | @nielsberglund

    Monday, June 24, 2019 3:37 AM
    Moderator