0

I apologize if this is super easy, I'm still new to SQL and learning everyday. What I'm trying to do is query

SELECT ProjectName,
       RequestNumber,
       ContactId,
       ContactFirstName,
       ContactLastName,
       RequestReceivedDate,
       RequestCloseDate,
       Category,
       SubCategory,
       AssignedResourceId,
       OwningResourceId,
       RequestSource,
       ClosedImmediatelyFlag,
       RequestStatusCode
FROM dbo.system_requests;

BUT my RequestReceivedDate and RequestCloseDate, are off by 5 hours. I can't create a function as I'm working with a vendor database so looking to convert UTC to local time in my select statement and just not sure the best way of doing this?

Thoughts?

jarlh
  • 42,561
  • 8
  • 45
  • 63

2 Answers2

0

SQL Server 2016 has support for time zones with the AT TIME ZONE 'UTC' statement. You can do this:

SELECT ProjectName,
       RequestNumber,
       ContactId,
       ContactFirstName,
       ContactLastName,
       RequestReceivedDate AT TIME ZONE 'UTC',
       RequestCloseDate AT TIME ZONE 'UTC',
       Category,
       SubCategory,
       AssignedResourceId,
       OwningResourceId,
       RequestSource,
       ClosedImmediatelyFlag,
       RequestStatusCode
FROM dbo.system_requests;

You can search for "AT TIME ZONE" on Google.

0

So I ended up doing this and it worked and gave me what i wanted:

           SELECT CONVERT(
                             DATETIME,
                             SWITCHOFFSET(
                                             CONVERT(DATETIMEOFFSET, system_requests.RequestReceivedDate),
                                             DATENAME(TzOffset, SYSDATETIMEOFFSET())
                                         )
                         ) AS RequestReceivedDate
       ),
       (
           SELECT CONVERT(
                             DATETIME,
                             SWITCHOFFSET(
                                             CONVERT(DATETIMEOFFSET, system_requests.RequestCloseDate),
                                             DATENAME(TzOffset, SYSDATETIMEOFFSET())
                                       )
                         ) AS RequestClosedDate