SQL Server SYSDATETIME() Function
The SYSDATETIME()
function in SQL Server returns the current system date and time. It is similar to the GETDATE()
function, but with millisecond-level precision, making it useful for scenarios that require high-precision date and time values.
Syntax
SYSDATETIME ( )
Usage
The SYSDATETIME()
function is typically used in applications that require precise timestamps and tasks that require high-precision date and time values.
Examples
Example 1
The following example shows how to use the SYSDATETIME()
function to retrieve the current date and time with millisecond-level precision.
SELECT SYSDATETIME() AS 'CurrentDateTime'
Executing the SQL statement above will return the current system date and time in the following format:
CurrentDateTime |
---|
2023-03-11 06:29:27.4670125 |
Example 2
The following example shows how to use the SYSDATETIME()
function in an INSERT statement to insert the current date and time with millisecond-level precision.
CREATE TABLE ExampleTable (
ID INT PRIMARY KEY,
Col1 VARCHAR(50),
CreatedAt DATETIME2(7)
)
INSERT INTO ExampleTable (ID, Col1, CreatedAt)
VALUES (1, 'Example Value', SYSDATETIME())
Executing the SQL statement above will insert a row into the ExampleTable
table, where the value in the CreatedAt
column is the current system date and time with millisecond-level precision in the following format:
ID | Col1 | CreatedAt |
---|---|---|
1 | Example Value | 2023-03-11 06:39:18.9279424 |
Conclusion
The SYSDATETIME()
function is a high-precision date and time function in SQL Server that can return the current system date and time with millisecond-level precision. It is useful for applications that require precise timestamps and tasks that require high-precision date and time values.