2016-01-06 126 views
0

我需要將數據庫日期中的一個日期列轉換爲正好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 
+0

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 –

+0

什麼樣的報告消耗此查詢?這可能是更容易返回原UTC時間(減一天)則在報告中,類似於它是如何在你的應用程序進行執行時區轉換。 –

+0

您好,Esoteric可以請您發佈代碼示例行,如果我想運行基於這一列的時間戳後一天的報告,我該怎麼寫? – Tony77

回答

0

一個簡單的方法來完成這一點是創建一個caluated列,然後獲取報告引用這個新列。這樣,您可以保留原始數據,並仍然通過數據庫處理轉換。

ALTER TABLE [Your Table Name] ADD 
UTC2EST AS dateadd(hh,-5,[Your current UTC datetime column]) 
+0

如果我想運行的基礎上改建場的前一天的報告 - 1? – Tony77

+0

我會搞定,支持報告中的存儲過程的where子句。 Some SomeDate = DateAdd(dd,-1,MyDate) – bsivel

相關問題