SQL Server - System Datetime

Getting Current Date Time From SQL Server

There are a few ways to get the current datetime in SQL Server. Knowing how to do this becomes important when you need to perform date math against other datetime columns at query runtime. For example you might need to

The difference between CURRENT_TIMESTAMP and GETDATE()

CURRENT_TIMESTAMP is the ANSI verb. GETDATE() is the T-SQL function. Both are returned without the server timezone offset included. In fact, there is no difference between the two. If you create a table referening CURRENT_TIMESTAMP as a default then view the table schema you will see GETDATE() in the returned code! So, if you are building tables where you need to build across different RDBMS's you may want to use the ANSI view in your initial create table code as it is one less thing you will need to tweak. Otherwise, it does not matter which one you use.

SELECT TOP 1
  GETDATE()     AS "GETDATE()" --  T-SQL method, Time zone offset not included
 ,CURRENT_TIMESTAMP AS "CURRENT_TIMESTAMP" --  ANSI, Time zone offset not included
 ,GETUTCDATE() AS "GETUTCDATE()" -- --UTC time
-- Note precision of dates below 
 ,SYSDATETIME() AS "SYSDATETIME()" --  Time zone offset not included
 ,SYSUTCDATETIME() AS "SYSUTCDATETIME()" --UTC time 
FROM AdventureWorks2008R2.Sales.Store

sysdatetime

 

If you require greater precision in time you can use the SYSDATETIME() function.



 Collapse All    Expand All
Custom Search

SQL Server Information and Resources