SQL Server equivalent of MySQL's NOW()?

SqlSql Server

Sql Problem Overview


I'm a MySQL guy working on a SQL Server project, trying to get a datetime field to show the current time. In MySQL I'd use NOW() but it isn't accepting that.

INSERT INTO timelog (datetime_filed) VALUES (NOW())

Sql Solutions


Solution 1 - Sql

getdate() or getutcdate().

Solution 2 - Sql

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

Solution 3 - Sql

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.

Solution 4 - Sql

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 ...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew G. JohnsonView Question on Stackoverflow
Solution 1 - SqlDaniel SchafferView Answer on Stackoverflow
Solution 2 - SqlSteven A. LoweView Answer on Stackoverflow
Solution 3 - SqlDMKView Answer on Stackoverflow
Solution 4 - SqlIan VarleyView Answer on Stackoverflow