SQL Server 2008 UTC转换为EST

时间:2016-01-06 19:19:54

标签: sql sql-server-2008 tsql datetime utc

我需要将数据库日期中的一个日期列转换为正好5个小时。时间保持为UTC,当他们查看应用程序时,它会显示EST时区。该应用程序自动转换时间。 UTC比东部标准时间早5点。以下代码似乎可以修复大多数时间戳的时间和日期,但有没有办法设置和偏移?该报告在一天后查看,并应将转换后的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

1 个答案:

答案 0 :(得分:0)

实现此目的的一种简单方法是创建一个经过加密的列,然后获取报告以引用此新列。这样您就可以保留原始数据并仍然通过数据库处理转换。

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