Convert UTC Time to Other Time Zones
You have tables used in reporting that have CreatedDate and ModifiedDate columns. You want to report activity at certain times of the day; e.g. 10AM, 1PM, and 4PM. The report users are in different time zones. How can you handle this?
Use DATETIMEOFFSET as the column type for the CreatedDate and ModifiedDate columns; e.g.
CreatedTime DATETIMOFFSET(0),
ModifiedTime DATETIMEOFFSET(0)
When you set the values for these columns use <column name> = GETUTCDATE()
You will see values like this: 2021-01-08 20:14:52 +00:00
When a user is running a report, they should see the CreatedDate or ModifiedDate in their time zone.
You can do that easily by associating the user with a time zone; e.g. use the time zone of their office; e.g.
SELECT ModifiedDate AT TIME ZONE 'Eastern Standard Time' and you'll see this: 2021-01-08 15:14:52 -05:00
To see the list of time zones:
SELECT *
FROM sys.time_zone_info
Comments
Post a Comment