,

How To check the time zone in Azure SQL Server

Posted by

In Azure SQL Server, the time zone is generally set to UTC (Coordinated Universal Time), and it cannot be changed at the server or database level like on-premises SQL Server instances. However, you can determine the current time zone or manipulate time zone-related data by querying system functions or using time zone conversion functions.

How to Determine Time Zone in Azure SQL:

  1. Check the Current System Time (Default UTC): Since Azure SQL Server uses UTC by default, you can query the current time using the SYSDATETIME() or GETUTCDATE() function:
-- Get the current system time in UTC
SELECT SYSDATETIME() AS CurrentDateTimeUTC;

or

-- Get the current UTC time
SELECT GETUTCDATE() AS CurrentUTCDateTime;

Check Server Time Zone Using sys.time_zone_info: You can use the sys.time_zone_info system view to list all the supported time zones in Azure SQL:

-- List all supported time zones in SQL Server
SELECT * FROM sys.time_zone_info;

This query will show you all time zones available for use with the AT TIME ZONE function.

Converting UTC to a Specific Time Zone: If you need to work with different time zones, you can use the AT TIME ZONE function to convert the UTC time to your desired time zone. For example, if you want to convert the current time to the Eastern Standard Time (EST), you can do this:

-- Convert UTC time to Eastern Standard Time (EST)
SELECT SYSDATETIME() AT TIME ZONE 'Eastern Standard Time' AS ESTDateTime;

Similarly, you can replace 'Eastern Standard Time' with any other time zone listed in sys.time_zone_info.

Time Zone Offset Calculation: You can also find the offset from UTC for specific time zones using the AT TIME ZONE function, which includes both the local time and the offset:

-- Get the current time in UTC and offset for a specific time zone
SELECT SYSDATETIME() AT TIME ZONE 'Pacific Standard Time' AS PSTDateTime,
       SYSDATETIME() AT TIME ZONE 'Central European Standard Time' AS CESTDateTime;

Example: Converting UTC to Different Time Zones

Here’s how you can convert the current UTC time to various time zones:

-- Convert current UTC time to multiple time zones
SELECT 
    SYSDATETIME() AT TIME ZONE 'UTC' AS UTCDateTime,
    SYSDATETIME() AT TIME ZONE 'Eastern Standard Time' AS ESTDateTime,
    SYSDATETIME() AT TIME ZONE 'Pacific Standard Time' AS PSTDateTime,
    SYSDATETIME() AT TIME ZONE 'India Standard Time' AS ISTDateTime;

Summary:

  • Azure SQL Server runs on UTC by default.
  • Use the AT TIME ZONE function to convert UTC to other time zones.
  • Query sys.time_zone_info for a list of supported time zones.
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x