Tuesday, February 26, 2013

T-SQL: Get current date and time

Functions available to get the current date and time

Function Result Return type
GETDATE() 2013-02-26 20:54:24.057 DATETIME
CURRENT_TIMESTAMP 2013-02-26 20:54:24.057 DATETIME
GETUTCDATE() 2013-02-26 20:09:17.467 DATETIME
SYSDATETIME() 2013-02-26 21:09:17.4680514 DATETIME2
SYSUTCDATETIME() 2013-02-26 20:09:17.4680514 DATETIME2
SYSDATETIMEOFFSET() 2013-02-26 21:09:17.4680514 +01:00 DATETIMEOFFSET

UTC means Coordinated Universal Time and is widely used as time standard in web applications. The result above were taken on a PC with the time zone (UTC+01:00). Note that the UTC functions return the time one hour before the rest.

CURRENT_TIMESTAMP is the recommended one as it is the ANSI equivalent of GETDATE()

Don't forget that if you only need the current DATE or TIME, you can cast the return of the above functions to a DATE or TIME

Example:

SELECT CAST(GETDATE() as DATE)

which will return

2013-02-26

For all DateTime functions visit the following link

No comments:

Post a Comment