TODATETIMEOFFSET (Transact-SQL)TODATETIMEOFFSET (Transact-SQL)

適用於: 是SQL Server (從 2008 開始) 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server (starting with 2008) yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

傳回從 datetime2 運算式翻譯的 datetimeoffset 值。Returns a datetimeoffset value that is translated from a datetime2 expression.

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

語法Syntax

TODATETIMEOFFSET ( expression , time_zone )  

引數Arguments

expressionexpression
為解析為 datetime2 值的運算式Is an expression that resolves to a datetime2 value.

注意

運算式的類型不可為 textntextimage,因為這些類型不可隱含轉換成 varcharnvarcharThe expression cannot be of type text, ntext, or image because these types cannot be implicitly converted to varchar or nvarchar.

time_zonetime_zone
這是代表時區時差的運算式,以分鐘為單位 (若為整數),例如 -120,或以小時和分鐘為單位 (若為字串),例如 '+13:00'。Is an expression that represents the time zone offset in minutes (if an integer), for example -120, or hours and minutes (if a string), for example '+13:00'. 範圍是 +14 到 -14 (以小時為單位)。The range is +14 to -14 (in hours). 此運算式會針對指定的 time_zone 以當地時間解譯。The expression is interpreted in local time for the specified time_zone.

注意

如果運算式為字元字串,它的格式必須為 {+|-}TZH:THM。If expression is a character string, it must be in the format {+|-}TZH:THM.

傳回類型Return Type

datetimeoffset.datetimeoffset. 毫秒精確度與 datetime 引數相同。The fractional precision is the same as the datetime argument.

範例Examples

A.A. 變更目前日期和時間的時區時差Changing the time zone offset of the current date and time

下列範例會將目前日期和時間的時區時差變更為 -07:00 時區。The following example changes the zone offset of the current date and time to time zone -07:00.

DECLARE @todaysDateTime datetime2;  
SET @todaysDateTime = GETDATE();  
SELECT TODATETIMEOFFSET (@todaysDateTime, '-07:00');  
-- RETURNS 2019-04-22 16:23:51.7666667 -07:00  

B.B. 變更時區時差 (以分鐘為單位)Changing the time zone offset in minutes

下列範例會將目前的時區變更為 -120 分鐘。The following example changes the current time zone to -120 minutes.

SELECT TODATETIMEOFFSET(SYSDATETIME(), -120)
-- RETURNS: 2019-04-22 11:39:21.6986813 -02:00  

C.C. 在時區時差中增加 13 小時的時間Adding a 13-hour time zone offset

下列範例會在日期和時間中加入 13 小時的時區時差。The following example adds a 13-hour time zone offset to a date and time.

SELECT TODATETIMEOFFSET(SYSDATETIME(), '+13:00')
-- RETURNS: 2019-04-22 11:39:29.0339301 +13:00

另請參閱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)