SYSDATETIME (Transact-SQL)SYSDATETIME (Transact-SQL)

適用於: 是SQL Server 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

傳回 datetime2(7) 值,此值包含在其上執行 SQL ServerSQL Server 執行個體之電腦的日期和時間。Returns a datetime2(7) value that contains the date and time of the computer on which the instance of SQL ServerSQL Server is running.

注意

SYSDATETIME 和 SYSUTCDATETIME 比 GETDATE 和 GETUTCDATE 具有更多小數秒數有效位數。SYSDATETIME and SYSUTCDATETIME have more fractional seconds precision than GETDATE and GETUTCDATE. SYSDATETIMEOFFSET 包含系統時區位移。SYSDATETIMEOFFSET includes the system time zone offset. SYSDATETIME、SYSUTCDATETIME 和 SYSDATETIMEOFFSET 可指派給任何日期和時間類型的變數。SYSDATETIME, SYSUTCDATETIME, and SYSDATETIMEOFFSET can be assigned to a variable of any of the date and time types.

如需所有 Transact-SQLTransact-SQL 日期和時間資料類型與函數的概觀,請參閱日期和時間資料類型與函數 (Transact-SQL)For an overview of all Transact-SQLTransact-SQL date and time data types and functions, see Date and Time Data Types and Functions (Transact-SQL).

主題連結圖示 Transact-SQL 語法慣例Topic link icon Transact-SQL Syntax Conventions

語法Syntax

SYSDATETIME ( )  

傳回類型Return Type

datetime2(7)datetime2(7)

RemarksRemarks

只要是 Transact-SQLTransact-SQL 陳述式可以參考 datetime2(7) 運算式的任何位置,它們就可以參考 SYSDATETIME。Transact-SQLTransact-SQL statements can refer to SYSDATETIME anywhere they can refer to a datetime2(7) expression.

SYSDATETIME 是不具決定性的函數。SYSDATETIME is a nondeterministic function. 在資料行中參考這個函數的檢視表和運算式無法編製索引。Views and expressions that reference this function in a column cannot be indexed.

注意

SQL ServerSQL Server 會使用 GetSystemTimeAsFileTime() Windows API 來取得日期和時間值。obtains the date and time values by using the GetSystemTimeAsFileTime() Windows API. 精確度取決於執行 SQL ServerSQL Server 執行個體的電腦硬體和 Windows 版本。The accuracy depends on the computer hardware and version of Windows on which the instance of SQL ServerSQL Server is running. 此 API 的精確度是固定於 100 奈秒。The precision of this API is fixed at 100 nanoseconds. 正確性可藉由使用 GetSystemTimeAdjustment() Windows API 來判斷。The accuracy can be determined by using the GetSystemTimeAdjustment() Windows API.

範例Examples

下列範例會使用六個可傳回目前日期和時間的 SQL ServerSQL Server 系統函數來傳回日期、時間或這兩者。The following examples use the six SQL ServerSQL Server system functions that return current date and time to return the date, time or both. 由於這些值會依序傳回,因此其小數秒數可能會不同。The values are returned in series; therefore, their fractional seconds might be different.

A.A. 取得目前的系統日期和時間Getting the current system date and time

SELECT SYSDATETIME()  
    ,SYSDATETIMEOFFSET()  
    ,SYSUTCDATETIME()  
    ,CURRENT_TIMESTAMP  
    ,GETDATE()  
    ,GETUTCDATE();  
/* Returned:  
SYSDATETIME()      2007-04-30 13:10:02.0474381  
SYSDATETIMEOFFSET()2007-04-30 13:10:02.0474381 -07:00  
SYSUTCDATETIME()   2007-04-30 20:10:02.0474381  
CURRENT_TIMESTAMP  2007-04-30 13:10:02.047  
GETDATE()          2007-04-30 13:10:02.047  
GETUTCDATE()       2007-04-30 20:10:02.047  
*/

B.B. 取得目前的系統日期Getting the current system date

SELECT CONVERT (date, SYSDATETIME())  
    ,CONVERT (date, SYSDATETIMEOFFSET())  
    ,CONVERT (date, SYSUTCDATETIME())  
    ,CONVERT (date, CURRENT_TIMESTAMP)  
    ,CONVERT (date, GETDATE())  
    ,CONVERT (date, GETUTCDATE());  
  
/* All returned 2007-04-30 */  

C.C. 取得目前的系統時間Getting the current system time

SELECT CONVERT (time, SYSDATETIME())  
    ,CONVERT (time, SYSDATETIMEOFFSET())  
    ,CONVERT (time, SYSUTCDATETIME())  
    ,CONVERT (time, CURRENT_TIMESTAMP)  
    ,CONVERT (time, GETDATE())  
    ,CONVERT (time, GETUTCDATE());  
  
/* Returned  
SYSDATETIME()      13:18:45.3490361  
SYSDATETIMEOFFSET()13:18:45.3490361  
SYSUTCDATETIME()   20:18:45.3490361  
CURRENT_TIMESTAMP  13:18:45.3470000  
GETDATE()          13:18:45.3470000  
GETUTCDATE()       20:18:45.3470000  
*/  

範例:Azure SQL 資料倉儲Azure SQL Data Warehouse平行處理資料倉儲Parallel Data WarehouseExamples: Azure SQL 資料倉儲Azure SQL Data Warehouse and 平行處理資料倉儲Parallel Data Warehouse

D.取得目前的系統日期和時間D: Getting the current system date and time

SELECT SYSDATETIME();  

以下為結果集:Here is the result set.

--------------------------  
7/20/2013 2:49:59 PM

另請參閱See Also

CAST 和 CONVERT (Transact-SQL) CAST and CONVERT (Transact-SQL)
日期和時間資料類型與函數 (Transact-SQL)Date and Time Data Types and Functions (Transact-SQL)