我需要將數據庫日期中的一個日期列轉換爲正好5個小時。時間保持爲UTC,當他們一個人查看它的應用程序時,它會顯示EST時區。該應用程序自動轉換時間。 UTC比東部標準時間早5點。下面的代碼然而似乎定爲最時間戳的時間和日期,是否有一種方法來設置和偏移?該報告在有朝一日回顧並應該將轉換後的UTC返回到EST時間。SQL Server 2008 UTC轉換爲EST
SELECT DISTINCT
Encounters.EncounterNo, MedicalRecords.MedRecNo as MRN,
DATEADD(DAY, -1, DATEADD(hh, DATEDIFF(hh, GETDATE(), GETUTCDATE()), Documents.CreateDateTime)) AS [Create Date],
SUBSTRING(Enrollees.EnrolleeName, 1, CHARINDEX(',', Enrollees.EnrolleeName) - 1) as [Last Name],
SUBSTRING(Enrollees.EnrolleeName, CHARINDEX(',', Enrollees.EnrolleeName) + 1, len(Enrollees.EnrolleeName)) as [First Name],
DocTypes.DocTypeName,
CAST(Enrollees.BirthDate AS Date) AS [BIRTH DATE],
Enrollees.Gender, Enrollees.SocSecNo, Enrollees.Address,
CAST(Encounters.EncntrStartDate AS Date) AS [ADMIT DATE]
FROM
Enrollees
INNER JOIN
MedicalRecords ON Enrollees.EnrolleeOwnerId = MedicalRecords.EnrolleeOwnerId
INNER JOIN
Documents
INNER JOIN
DocTypes ON Documents.DocType = DocTypes.DocType
INNER JOIN
DocsOwners ON Documents.DocId = DocsOwners.DocId
INNER JOIN
Encounters ON DocsOwners.OwnerId = Encounters.EncntrOwnerId
ON MedicalRecords.MedRecOwnerId = Encounters.MedRecOwnerId
WHERE
Documents.DocType = '65'
AND (DATEDIFF(DD, GETUTCDATE(), Documents.CreateDateTime) = - 1)
ORDER BY
DocTypes.DocTypeName
http://www.sqlservercentral.com/Forums/Topic643108-8-1.aspx 您可能必須考慮到日光節約時間太長。 https://social.msdn.microsoft.com/forums/sqlserver/en-US/5435096c-7b7a-4d8a-8933-ab3b987f2b21/date-conversion-utc-to-est-with-day-light-saving –
什麼樣的報告消耗此查詢?這可能是更容易返回原UTC時間(減一天)則在報告中,類似於它是如何在你的應用程序進行執行時區轉換。 –
您好,Esoteric可以請您發佈代碼示例行,如果我想運行基於這一列的時間戳後一天的報告,我該怎麼寫? – Tony77