Question

I have a table in database and it has a column named 'Modified date'. Whenever i edit some data from the form in asp.net web form i should be able to fill the database table column 'Modified date' with the current date automatically. Is there any way out for it.I am stuck with it.

Was it helpful?

Solution

set

getdate()

something like this:

ALTER TABLE [TableName] ADD  DEFAULT (getdate()) FOR [ColumnName]
Go

as default in your table will give you current insert date and time for Sql server

There are two possible ways :

  1. From your code behind
  2. From Select Query from Database

example to select only date from date time :

//from code behind
Variable_Name = DateTime.Now.ToString("dd/MM/yyyy");

or

-- from Database query
SELECT CONVERT (DATE, GETDATE())

OTHER TIPS

You can do it either as Krunal says or hardcode in your insert query in C# (or VB):

Insert into..... values ( ...., SELECT convert(varchar, getdate(), 106),...,)

this inserts into format: dd/mmm/yy

You can use GETDATE() function!

Set default constraint on your DateTime field that will cause the current date/time to be inserted when you insert a new row.

To set the date when updates are made you can use a Trigger.

CREATE TRIGGER trgYourTableUpdateTimestamp
  ON dbo.YourTable FOR UPDATE
AS BEGIN
   UPDATE 
      dbo.YourTable 
   SET 
      YourTimeStampColumn = GETDATE()
   FROM 
      Inserted Ins
   WHERE
      dbo.YourTable.SomeUniqueId = Ins.SomeUniqueId
END
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top