Blog Post

Azure Database Support Blog
1 MIN READ

Lesson Learned #4: Modifying the default time zone for your local time zone.

Jose_Manuel_Jurado's avatar
Mar 14, 2019
First published on MSDN on Jul 27, 2016


Currently, the default time zone on Azure SQL DB is UTC . Unfortunately, there is not possible to change by server configuration or database configuration.

All Azure services use UTC time zone settings, regardless of their physical location. This also applies for SQL Azure, so if you need a custom time zone you'll have to manage that in the middle/front end tier.

One suggestion that we have is to review the new date/time function that AZURE SQL DATABASE provided as AT TIME ZONE and datetimeoffset you could find out the different time zones running the query: select * from sys.time_zone_info

For example, we could create the following function:

CREATE FUNCTION dReturnDate( @dFecha as datetime)


returns DATETIME


as


begin


DECLARE @D AS datetimeoffset


SET @D = CONVERT(datetimeoffset, @Dfecha) AT TIME ZONE 'W. Europe Standard Time'


RETURN CONVERT(datetime, @D);


end


Running the query DBO.dReturnDate(getdate()) you could observe the results. Please, verify the timezone that you are working on with select * from sys.time_zone_info.

Updated Mar 14, 2019
Version 2.0
  • GarryGBain's avatar
    GarryGBain
    Copper Contributor

    I created a simple function that returns the correct UK time whether in DST or not.
    This can be adapted for different time zones.

    CREATE FUNCTION [dbo].[f_CurrentDateTime]() RETURNS DATETIME AS
    BEGIN
    RETURN DATEADD(HOUR,CONVERT(INT,(SELECT is_currently_dst FROM sys.time_zone_info WHERE 1=1 AND NAME = 'GMT Standard Time')),GETDATE())
    END

  • NagaPakalapati's avatar
    NagaPakalapati
    Copper Contributor

    If there is an absolute need to change the timezone, there is a workaround mentioned in the docs.

     

    • The time zone of the existing managed instance can't be changed. As a workaround, create a new managed instance with the proper time zone and then either perform a manual backup and restore, or what we recommend, perform a cross-instance point-in-time restore.

    https://docs.microsoft.com/en-us/azure/azure-sql/managed-instance/timezones-overview#limitations

     

  • keiryliza's avatar
    keiryliza
    Copper Contributor

    It works good, change all queries getdate() to dbo.dReturnDate(getdate()) dependy of your local time.

     

    For example in Nicaragua is:

     

    create FUNCTION dReturnDate( @dFecha as datetime)
    returns DATETIME
    as
    begin
    DECLARE @D AS datetimeoffset
    SET @D = CONVERT(datetimeoffset, @Dfecha) AT TIME ZONE 'Central America Standard Time'
    RETURN CONVERT(datetime, @D);
    end


    select dbo.dReturnDate(getdate())

  • Interesting way around it, but running it through a function could cause performance issues, as shown by Jonathon Kehayias elsewhere. It's frustrating. 

     

  •  

    keiryliza : It works good, change all queries getdate() to dbo.dReturnDate(getdate()) dependy of your local time.

    This sound like really bad idea! Using scalar function can reduce performance dramatically in many cases.

     

    Instead simply use the inline code:

     

    select CONVERT(datetime, CONVERT(datetimeoffset, getdate()) AT TIME ZONE 'Israel Standard Time')

     

    NagaPakalapati : If there is an absolute need to change the timezone, there is a workaround mentioned in the docs.

    You confuse two different services. The post is about Azure SQL Database and you speak about Azure Managed Instance 🙂

     

    Jose_Manuel_Jurado : the default time zone on Azure SQL DB is UTC

    Coordinated Universal Time (UTC) is a time standard and NOT a time zone, Greenwich Mean Time (GMT) is a time zone. This entire discussion and many of the documentations and the terms used by Microsoft should be speaking about GMT time zone and NOT about UTC.