SQL Server has a number of built-in functions that can be used to retrieve the current date in various formats. The following is a list of those functions, along with an explanation of the differences between them.
SELECT CURRENT_TIMESTAMP
This returns the current date in “YYYY-MM-DD HH:MM:SS.SSS”
Example: 2022-02-20 12:00:57.117
SELECT GETDATE()
This returns the current date in “YYYY-MM-DD HH:MM:SS.SSS”
Example: 2022-02-20 12:00:57.117
If you notice, there is no difference between CURRENT_TIMESTAMP and GETDATE(). It’s because GETDATE is a T-SQL implementation of the same function as CURRENT TIMESTAMP, which is an ANSI SQL function. The operating system of the computer on which SQL Server is running provides value to both of them.
SELECT SYSDATETIME()
This returns the current date in “YYYY-MM-DD HH:MM:SS.SSSSSSS”
Example: 2022-02-20 12:08:40.3371389
SELECT SYSUTCDATETIME()
This returns the current date in “YYYY-MM-DD HH:MM:SS.SSSSSSS” in UTC.
Example: 2022-02-20 08:08:40.3371389
If you observe, the time zone in which I live is currently 12 (noon), whereas UTC time is -4 hours.
SELECT SYSDATETIMEOFFSET()
This returns the current date of the current location’s time in “YYYY-MM-DD HH:MM:SS.SSS” format, as well as the difference from UTC. The time zone offset is included.
Example: 2022-02-20 12:08:40.3371389 +04:00
SELECT GETUTCDATE()
This is similar to GETDATE() however it returns UTC date time.
Example: 2022-02-20 08:08:40.337
2 comments