The NOW() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS.
SQL Server provides several different functions that return the current date time including: GETDATE(), SYSDATETIME(), and CURRENT_TIMESTAMP.
SQL Server GETDATE() Function The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss.
In DDL statements, use rowversion instead of timestamp wherever possible. For more information, see Data Type Synonyms (Transact-SQL).
getdate()
or getutcdate()
.
getdate()
is the direct equivalent, but you should always use UTC datetimes
getutcdate()
whether your app operates across timezones or not - otherwise you run the risk of screwing up date math at the spring/fall transitions
SYSDATETIME()
and SYSUTCDATETIME()
are the DateTime2 equivalents of
GetDate()
and GetUTCDate()
which return a DateTime.
DateTime2 is now the preferred method for storing the date and time in SQL Server 2008+. See the following StackOverflow Post.
You can also use CURRENT_TIMESTAMP
, if you feel like being more ANSI compliant (though if you're porting code between database vendors, that'll be the least of your worries). It's exactly the same as GetDate()
under the covers (see this question for more on that).
There's no ANSI equivalent for GetUTCDate()
, however, which is probably the one you should be using if your app operates in more than a single time zone ...
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