AT TIME ZONE(Transact-SQL)
적용 대상: SQL Server 2016(13.x) 이상
Azure SQL 데이터베이스
Azure SQL Managed Instance
Azure Synapse Analytics
Microsoft Fabric에서 SQL 분석 엔드포인트
Microsoft Fabric 내 Warehouse
inputdate를 대상 표준 시간대의 해당 datetimeoffset 값으로 변환합니다. 오프셋 정보 없이 inputdate가 제공되면 이 함수는 inputdate가 대상 표준 시간대에서 있다고 가정하여 표준 시간대의 오프셋을 적용합니다. inputdate가 datetimeoffset 값 AT TIME ZONE
으로 제공되는 경우 절은 표준 시간대 변환 규칙을 사용하여 대상 표준 시간대로 변환합니다.
AT TIME ZONE
구현은 Windows 메커니즘을 사용하여 표준 시간대 간에 날짜/시간 값을 변환합니다.
구문
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 보기를 통해 노출됩니다.
SQL Server on Linux의 표준 시간대에 대한 자세한 내용은 Linux에서 SQL Server 2022에 대한 표준 시간대 구성을 참조하세요.
반환 형식
datetimeoffset의 데이터 형식을 반환합니다.
반환 값
대상 표준 시간대의 datetimeoffset 값입니다.
설명
AT TIME ZONE
는 DST 변경의 영향을 받는 간격에 속하는 smalldatetime, datetime 및 datetime2 데이터 형식의 입력 값을 변환하기 위한 특정 규칙을 적용합니다.
시간이 미리 설정되어 있으면 현지 시간에는 시간 조정 기간과 동일한 간격이 있습니다. 이 기간은 일반적으로 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. 다른 표준 시간대 사이의 값 변환
다음 예제에서는 다른 표준 시간대 사이의 값을 변환합니다. 값은 OrderDate
datetime이며 오프셋과 함께 저장되지 않지만 Pacific Standard Time으로 알려져 있습니다. 첫 번째 단계는 알려진 오프셋을 할당한 다음, 새 표준 시간대로 변환하는 것입니다.
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;
관련 콘텐츠
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기