SYSUTCDATETIME (Transact-SQL)SYSUTCDATETIME (Transact-SQL)

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

傳回 datetime2 值,此值包含在其上執行 SQL ServerSQL Server 執行個體之電腦的日期和時間。Returns a datetime2 value that contains the date and time of the computer on which the instance of SQL ServerSQL Server is running. 日期和時間是以國際標準時間 (Coordinated Universal Time,UTC) 傳回。The date and time is returned as UTC time (Coordinated Universal Time). 毫秒的有效位數規格範圍介於 1 至 7 位數之間。The fractional second precision specification has a range from 1 to 7 digits. 預設有效位數是 7 位數。The default precision is 7 digits.

注意

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 one of the date and time types.

如需所有 Transact-SQLTransact-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 語法慣例Topic link icon Transact-SQL Syntax Conventions

語法Syntax

SYSUTCDATETIME ( )  

傳回類型Return Type

datetime2datetime2

RemarksRemarks

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

SYSUTCDATETIME 是不具決定性的函數。SYSUTCDATETIME 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. 顯示日期和時間函數所傳回的格式Showing the formats that are returned by the date and time functions

下列範例示範日期和時間函數所傳回的不同格式。The following example shows the different formats that are returned by the date and time functions.

SELECT SYSDATETIME() AS [SYSDATETIME()]  
    ,SYSDATETIMEOFFSET() AS [SYSDATETIMEOFFSET()]  
    ,SYSUTCDATETIME() AS [SYSUTCDATETIME()]  
    ,CURRENT_TIMESTAMP AS [CURRENT_TIMESTAMP]  
    ,GETDATE() AS [GETDATE()]  
    ,GETUTCDATE() AS [GETUTCDATE()];  

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

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. 將日期和時間轉換成日期Converting date and time to date

下列範例會顯示如何將日期和時間值轉換成 dateThe following example shows you how to convert date and time values to date.

SELECT CONVERT (date, SYSDATETIME())  
    ,CONVERT (date, SYSDATETIMEOFFSET())  
    ,CONVERT (date, SYSUTCDATETIME())  
    ,CONVERT (date, CURRENT_TIMESTAMP)  
    ,CONVERT (date, GETDATE())  
    ,CONVERT (date, GETUTCDATE());  

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

2007-04-30
2007-04-30
2007-04-30
2007-04-30
2007-04-30
2007-04-30

C.C. 將日期和時間值轉換成時間Converting date and time values to time

下列範例會顯示如何將日期和時間值轉換成 timeThe following example shows you how to convert date and time values to time.

DECLARE @DATETIME DATETIME = GetDate();
DECLARE @TIME TIME
SELECT @TIME = CONVERT(time, @DATETIME)
SELECT @TIME AS 'Time', @DATETIME AS 'Date Time'

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

Time             Date Time  
13:49:33.6330000 2009-04-22 13:49:33.633

另請參閱See Also

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