Calculate Timezoneoffset using TimezoneName

ssalun 26 Reputation points
2021-08-30T20:14:57.067+00:00

Hi All,

I am dealing with many timezones , I have a required to generate a SSRS report which uses the timezoneoffset based on the timezone name. The offset value returned should consider with or without DayLight Saving Time.

The input to the report is the date.

Can anyone please help me with the function to which i can pass date and it will return offset.

Input
Date: 2021-08-27
TimeZoneName- America/Chicago or Europe/Berlin

Output: Timezoneoffset -05/+1

Thanks.

SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
3,061 questions
Developer technologies | Transact-SQL
SQL Server | Other
{count} vote

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-08-30T21:15:34.89+00:00

    In T-SQL, you can do as in this example_

    DECLARE @tz nvarchar(100) = 'Pacific Standard Time'
    SELECT datepart(tzoffset, sysutcdatetime() AT TIME ZONE @tz)
    

    However, this requires that the names you have agree with those in the Windows registry, which is less likely. Rather you would have to Google around for a library to parse time-zone names, and all depending the source, you have to augment the solution you find.


  2. MelissaMa-MSFT 24,221 Reputation points
    2021-08-31T02:25:32.477+00:00

    Hi @SwapnilSalunke-4731,

    Welcome to Microsoft Q&A!

    Unfortunately SQL Server doesn't yet support IANA time zone identifiers (like America/Chicago) directly.

    For now, the best option is to convert from the IANA identifier to a corresponding Windows time zone identifier in your application layer, and store that in your SQL Server.

    Since your version is SQL Server 2012 which "AT TIME ZONE " does not work, you could refer below which is an alternative approach.

    -- all SQL Server versions  
    declare @utc_date datetime = getdate()  
    select @utc_date as utc_time_zone,   
      dateadd(hh, datediff(hh, getutcdate(), getdate()), @utc_date) as local_time_zone  
    

    In addition, it is also recommended to create and use a Calendar table.

    Aaron Bertrand covers this exact scenario in depth here. You could refer it and find out more solutions.

    Best regards,
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.