Hi all, I have created a table in which i have a DateTime Column . i need that i dont want to insert values in that column from the front end. i have created a trigger for that purpose. whenever i insert a row in that table the dateTime column must be updated with current date and time.

To show the current date and time we have
Select Getdate()

But how to use function in a insert command
Any idea plz

Recommended Answers

All 6 Replies

Insert Into aTable (SomeString, SomeDateTime) Values ('abc', GetDate())
Update aTable
Set SomeDateTime = GetDate()
commented: Your always quicker to the draw.. +1

thanks a lot sknake. Problem Solved

Just insert the getdate().

If you want specific date formats look up the CONVERT function.

Insert into Table (date)

if i need short date and time then wts the method for that

CONVERT(CHAR(10),GETDATE(),110) Will give you the format 02-05-2003 CONVERT(CHAR(10),GETDATE(),10) Will give you 02-05-03

The MSDN library will give you all the conversions. You can also create your own format if you wish with DATEPART functions.

If this is for insert only, then you'd be better off with a default value in the date column as you may not know that triggers run at a statement level, no a row level.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.