SQL Server has a bunch of functions that return the current date/time. Two of these are the GETDATE()
and SYSDATETIME()
functions.
At first glance, these two functions appear to do the same thing – get the current date and time from the operating system of the computer on which the instance of SQL Server is running.
However, there’s a subtle difference between the two.
The Difference? The Return Value Type
The main difference between GETDATE()
and SYSDATETIME()
is in the return value type.
GETDATE()
returns a datetime value.SYSDATETIME()
returns a datetime2(7) value.
This means that SYSDATETIME()
has more fractional seconds precision than GETDATE()
.
Both functions get the current date and time from the operating system of the computer on which the instance of SQL Server is running, but the fractional precision is different.
Example
Here’s an example to demonstrate the different values that are returned for each function:
SELECT GETDATE() AS GETDATE, SYSDATETIME() AS SYSDATETIME;
Result:
+-------------------------+-----------------------------+ | GETDATE | SYSDATETIME | |-------------------------+-----------------------------| | 2018-06-13 02:48:27.653 | 2018-06-13 02:48:27.6542371 | +-------------------------+-----------------------------+
So as mentioned, SYSDATETIME()
, which returns a datetime2(7) value, has a larger fractional precision than GETDATE()
, which returns a datetime value.
The datetime2 data type also has a larger date range and optional user-specified precision.
Which One Should I Use?
Microsoft recommends that we use datetime2 with our date/time values. This data type aligns with the SQL standard, and is more portable than datetime.
Therefore, use SYSDATETIME()
unless you have reason not to.