AT TIME ZONE(Transact-SQL)

적용 대상: Microsoft Fabric의 Microsoft FabricWarehouse에 있는 SQL Server 2016(13.x) 이상 Azure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsSQL 분석 엔드포인트

inputdate를 대상 표준 시간대의 해당 datetimeoffset 값으로 변환합니다. 오프셋 정보 없이 inputdate가 제공되면 이 함수는 inputdate가 대상 표준 시간대에서 있다고 가정하여 표준 시간대의 오프셋을 적용합니다. inputdate가 datetimeoffsetAT TIME ZONE 으로 제공되는 경우 절은 표준 시간대 변환 규칙을 사용하여 대상 표준 시간대로 변환합니다.

AT TIME ZONE구현은 Windows 메커니즘을 사용하여 표준 시간대 간에 날짜/시간 값을 변환합니다.

Transact-SQL 구문 표기 규칙

구문

inputdate AT TIME ZONE timezone

인수

inputdate

smalldatetime, datetime, datetime2 또는 datetimeoffset 값으로 확인할 수 있는 식입니다.

시간대

대상 표준 시간대의 이름입니다. SQL Server은 Windows 레지스트리에 저장된 표준 시간대를 따릅니다. 컴퓨터에 설치된 표준 시간대는 다음 레지스트리 하이브 KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones에 저장됩니다. 설치된 표준 시간대의 목록은 sys.time_zone_info(Transact-SQL) 보기를 통해 공개됩니다.

SQL Server on Linux의 표준 시간대에 대한 자세한 내용은 Linux에서 SQL Server 2022에 대한 표준 시간대 구성을 참조하세요.

반환 형식

datetimeoffset의 데이터 형식을 반환합니다.

반환 값

대상 표준 시간대의 datetimeoffset 값입니다.

설명

AT TIME ZONE 는 DST 변경의 영향을 받는 간격에 속하는 smalldatetime, datetimedatetime2 데이터 형식의 입력 값을 변환하기 위한 특정 규칙을 적용합니다.

  • 시간이 미리 설정되어 있으면 현지 시간에는 시간 조정 기간과 동일한 간격이 있습니다. 이 기간은 일반적으로 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 될 수 있으므로 함수는 비결정적 정보로 분류됩니다.

datetimeoffset은 Microsoft Fabric AT TIME ZONE 의 데이터 웨어하우징에서 지원되지 않지만 다음 예제와 같이 datetime2와 함께 계속 사용할 수 있습니다.

예제

A. 오프셋 정보 없는 날짜/시간에 대상 표준 시간대 오프셋 추가

원래 날짜/시간 값이 동일한 표준 시간대에 제공된 것을 알고 있는 경우 표준 시간대 규칙에 따라 오프셋을 추가하는 데 사용합니다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;

다음 단계