Set a DateTime database field to "Now"

Thibault Witzig picture Thibault Witzig · Dec 20, 2010 · Viewed 278.6k times · Source

In VB.net code, I create requests with SQL parameters. It I set a DateTime parameter to the value DateTime.Now, what will my request look like ?

UPDATE table SET date = "2010/12/20 10:25:00";

or

UPDATE table SET date = GETDATE();

In the first case I'm sure that every record will be set to the exact same time. In the second case it depends on how the DBMS processes the request. Which leads me to the second question : does SQL Server set the same date and time when updating a large table with NOW() ?

EDIT : replaced NOW() (which doesn't exist in SQL Server) by GETDATE().

Answer

Oded picture Oded · Dec 20, 2010

In SQL you need to use GETDATE():

UPDATE table SET date = GETDATE();

There is no NOW() function.


To answer your question:

In a large table, since the function is evaluated for each row, you will end up getting different values for the updated field.

So, if your requirement is to set it all to the same date I would do something like this (untested):

DECLARE @currDate DATETIME;
SET @currDate = GETDATE();

UPDATE table SET date = @currDate;