Summary: in this tutorial, you will learn how to use the SYSDATETIME() function to get the current system date and time.
SQL Server SYSDATETIME() function #
The SYSDATETIME() function returns a value of DATETIME2 that represents the current system date and time of the server on which the SQL Server instance is running.
The SYSDATETIME() function accepts no parameter:
SYSDATETIME()
Code language: SQL (Structured Query Language) (sql)Here is the output:
Result
---------------------------
2019-05-02 22:22:33.3975855
(1 row affected)
Code language: SQL (Structured Query Language) (sql)Note that the SYSDATETIME() function has more fractional seconds precision than the GETDATE() function.
The SYSDATETIME() is a nondeterministic function, therefore, views and columns that have expressions reference this function cannot be indexed.
SQL Server SYSDATETIME() function examples #
Let’s take some example of using the SYSDATETIME() function.
A) Returning the current system date example #
This example uses the CONVERT() function to convert the result of the SYSDATETIME() function to the current date:
SELECT
CONVERT(DATE, SYSDATETIME());
Code language: SQL (Structured Query Language) (sql)Here is the result:
Result
----------
2019-05-02
(1 row affected)
Code language: SQL (Structured Query Language) (sql)B) Returning the current system time #
The following example converts the result of the SYSDATETIME() function to the current time:
SELECT
CONVERT(TIME, SYSDATETIME()) Result;
Code language: SQL (Structured Query Language) (sql)The output is as follows:
Result
----------------
22:32:07.2066595
(1 row affected)
Code language: SQL (Structured Query Language) (sql)In this tutorial, you have learned how to use the SQL Server SYSDATETIME() function to get the current system date and time.