Thursday, April 18, 2024

T-SQL to Change UTC Time to Central Time

If you have been working with Azure SQL Server, you would have queried the sys.event_log table to check the logs of the server.  I needed to query this table to investigate the issue of an application failing to wake up the database in a paused state.  

Azure SQL Servers tend to be in UTC time format when you create them by default.

If you run a SELECT GETDATE(), it will return a UTC time following what the server is on.

My timezone is Central Standard Time.  I needed to convert UTC to CST.

Here's the code I used to do that:

SELECT GETDATE() AT TIME ZONE 'UTC' AT TIME ZONE 'Central Standard Time'


And back to the sys.event_log, here's the code I used, converting the [start_time] and [end_time] columns.

SELECT TOP (1000) [database_name]
      ,[start_time] AT TIME ZONE 'UTC' AT TIME ZONE 'Central Standard Time' AS [start_time]
      ,[end_time] AT TIME ZONE 'UTC' AT TIME ZONE 'Central Standard Time' AS [end_time]
      ,[event_category]
      ,[event_type]
      ,[event_subtype]
      ,[event_subtype_desc]
      ,[severity]
      ,[event_count]
      ,[description]
      ,[additional_data]
  FROM [sys].[event_log]
  ORDER BY [start_time] DESC
 

No comments:

Post a Comment

Little script to see the backup history

 Today, I was teaching a co-worker how to create a copy only backup of a SQL Managed Instance database.  And I came up to use this script t...