I have heard that DateTime.Now
is very expensive call (from here)
Is GETDATE()
in SQL 2005/2008 expensive? Have I to cache it into a variable if my stored procedure uses it a number of times?
To get the current date and time in SQL Server, use the GETDATE() function. This function returns a datetime data type; in other words, it contains both the date and the time, e.g. 2019-08-20 10:22:34 .
GETDATE returns the current date and time in the current session time zone (UTC by default). It returns the start date or time of the current statement, even when it is within a transaction block.
The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss.mmm' format.
GETDATE (SQL) A date/time function that returns the current local date and time.
It's not expensive: it comes straight from the OS.
I'd cache it anyway. It will most likely be different for separate calls if you have multiple statements. Say you have multiple inserts, surely you'd want the value to correlate acrosss tables?
If it's use in a SELECT, say, for output then it's only evaluated once usually.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With