SYSDATETIMEOFFSET (Transact-SQL)SYSDATETIMEOFFSET (Transact-SQL)

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

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

如需所有 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

SYSDATETIMEOFFSET ( )  

傳回類型Return Type

datetimeoffset(7)datetimeoffset(7)

RemarksRemarks

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

SYSDATETIMEOFFSET 是不具決定性的函數。SYSDATETIMEOFFSET 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 to times

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

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

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

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

另請參閱See Also

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