AT TIME ZONE (Transact-SQL)

適用於:Microsoft Fabric 中 Microsoft Fabric倉儲中的 SQL Server 2016 (13.x) 和更新版本 Azure SQL 資料庫 Azure SQL 受控執行個體 Azure Synapse AnalyticsSQL 分析端點

inputdate 轉換成目標時區中對應的 datetimeoffset 值。 提供 inputdate 但未提供位移資訊時,此函式就會在假設 inputdate 位於目標時區的情況下,套用時區位移。 如果提供 inputdate 來作為 datetimeoffset 值,則 AT TIME ZONE 子句會使用時區轉換規則將它轉換成目標時區。

AT TIME ZONE 實作需倚賴 Windows 機制來跨時區轉換 datetime 值。

Transact-SQL 語法慣例

語法

inputdate AT TIME ZONE timezone

引數

inputdate

可解析為下列值的運算式:smalldatetimedatetimedatetime2datetimeoffset

timezone

目的地時區的名稱。 SQL Server 依賴儲存在 Windows 登錄中的時區。 安裝於電腦上的時區均儲存於下列登錄區中:KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones。 已安裝的時區清單也會透過 sys.time_zone_info (Transact-SQL) 檢視公開。

如需 Linux 上的 SQL Server 時區的詳細資訊,請參閱在 Linux 上設定 SQL Server 2022 的時區

傳回類型

傳回 datetimeoffset 的資料類型。

傳回值

目標時區中的 datetimeoffset 值。

備註

AT TIME ZONE 會針對 smalldatetimedatetimedatetime2 資料類型中,落在受 DST 變更影響之間隔內的輸入值,套用特定的轉換規則:

  • 將時鐘調快時,本地時間會有落差,其相當於時鐘調整的持續時間。 這段持續時間通常是 1 個小時,但也可能是 30 或 45 分鐘,視時區而定。 將會使用在 DST 變更「後」之位移來轉換位於此落差中的時間點。

    /*
      Moving to DST in "Central European Standard Time" zone:
      offset changes from +01:00 -> +02:00
      Change occurred on March 27th, 2022 at 02:00:00.
      Adjusted local time became 2022-03-27 03:00:00.
    */
    
    --Time before DST change has standard time offset (+01:00)
    SELECT CONVERT(DATETIME2(0), '2022-03-27T01:01:00', 126)
    AT TIME ZONE 'Central European Standard Time';
    --Result: 2022-03-27 01:01:00 +01:00
    
    /*
      Adjusted time from the "gap interval" (between 02:00 and 03:00)
      is moved 1 hour ahead and presented with the summer time offset
      (after the DST change)
    */
    SELECT CONVERT(DATETIME2(0), '2022-03-27T02:01:00', 126)
    AT TIME ZONE 'Central European Standard Time';
    --Result: 2022-03-27 03:01:00 +02:00
    --Time after 03:00 is presented with the summer time offset (+02:00)
    SELECT CONVERT(DATETIME2(0), '2022-03-27T03:01:00', 126)
    AT TIME ZONE 'Central European Standard Time';
    --Result: 2022-03-27 03:01:00 +02:00
    
  • 將時鐘調整回來時,2 個小時的本地時間就會重疊成 1 個小時。 在此情況下,會使用在時鐘變更「之前」的位移來顯示屬於重疊間隔的時間點:

    /*
        Moving back from DST to standard time in
        "Central European Standard Time" zone:
        offset changes from +02:00 -> +01:00.
        Change occurred on October 30th, 2022 at 03:00:00.
        Adjusted local time became 2022-10-30 02:00:00
    */
    
    --Time before the change has DST offset (+02:00)
    SELECT CONVERT(DATETIME2(0), '2022-10-30T01:01:00', 126)
    AT TIME ZONE 'Central European Standard Time';
    --Result: 2022-10-30 01:01:00 +02:00
    
    /*
      Time from the "overlapped interval" is presented with DST offset (before the change)
    */
    SELECT CONVERT(DATETIME2(0), '2022-10-30T02:00:00', 126)
    AT TIME ZONE 'Central European Standard Time';
    --Result: 2022-10-30 02:00:00 +02:00
    
    
    --Time after 03:00 is regularly presented with the standard time offset (+01:00)
    SELECT CONVERT(DATETIME2(0), '2022-10-30T03:01:00', 126)
    AT TIME ZONE 'Central European Standard Time';
    --Result: 2022-10-30 03:01:00 +01:00
    

由於部分資訊 (例如時區規則) 的維護是在 SQL Server 外部進行且可能偶有變更,因此 AT TIME ZONE 函數會被歸類為不具決定性的函數。

雖然 Microsoft Fabric 中的數據倉儲不支援 datetimeoffsetAT TIME ZONE但仍可與 datetime2 搭配使用,如下列範例所示。

範例

A. 將目標時區位移新增至不含位移資訊的日期時間

當您知道在相同時區中已提供原始 datetime 值時,請使用 AT TIME ZONE 來根據時區規則新增位移:

USE AdventureWorks2022;
GO
  
SELECT SalesOrderID, OrderDate,
    OrderDate AT TIME ZONE 'Pacific Standard Time' AS OrderDate_TimeZonePST
FROM Sales.SalesOrderHeader;

B. 在不同的時區之間轉換值

下列範例會在不同的時區之間轉換值。 inputdate 值是 datetime,而且不會與位移一同儲存,但已知為「太平洋標準時間」。 第一個步驟是指派已知的位移,然後轉換為新時區:

USE AdventureWorks2022;
GO

SELECT SalesOrderID, OrderDate,
    --Assign the known offset only
    OrderDate AT TIME ZONE 'Pacific Standard Time' AS OrderDate_TimeZonePST,
    --Assign the known offset, then convert to another time zone
    OrderDate AT TIME ZONE 'Pacific Standard Time' AT TIME ZONE 'Central European Standard Time' AS OrderDate_TimeZoneCET
FROM Sales.SalesOrderHeader;

您也可以替換為包含時區的區域變數:

USE AdventureWorks2022;
GO

DECLARE @CustomerTimeZone nvarchar(128) = 'Central European Standard Time';

SELECT SalesOrderID, OrderDate,
    --Assign the known offset only
    OrderDate AT TIME ZONE 'Pacific Standard Time' AS OrderDate_TimeZonePST,
    --Assign the known offset, then convert to another time zone
    OrderDate AT TIME ZONE 'Pacific Standard Time' AT TIME ZONE @CustomerTimeZone AS OrderDate_TimeZoneCustomer
FROM Sales.SalesOrderHeader;

C. 使用特定時區查詢時態表

下列範例會從使用太平洋標準時間的時態表中選取資料。

USE AdventureWorks2022;
GO

DECLARE @ASOF DATETIMEOFFSET;

SET @ASOF = DATEADD(month, -1, GETDATE()) AT TIME ZONE 'UTC';

-- Query state of the table a month ago projecting period
-- columns as Pacific Standard Time
SELECT BusinessEntityID,
    PersonType,
    NameStyle,
    Title,
    FirstName,
    MiddleName,
    ValidFrom AT TIME ZONE 'Pacific Standard Time'
FROM Person.Person_Temporal
FOR SYSTEM_TIME AS OF @ASOF;

後續步驟