Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with Time Zones in SSRS

We store all our dates SQL Server 2008 database in UTC time in DateTime columns. I'm using SSRS to create reports and I need to convert all the times on the reports to the Time Zone of the computer where they're running the report from.

I know could always just pass in the current timezone offset as a parameter to the report and add or subtract the offset from the timezone, but this wouldn't correctly show historical dates because of daylight savings.

Does SSRS have any functions that handle this? Should I pass the timezone to the SQL server functions and have the SQL Server convert the time?

like image 583
jamiegs Avatar asked Jun 03 '10 20:06

jamiegs


People also ask

How do I select timezone in SQL?

For SQL Managed Instance, return value is based on the time zone of the instance itself assigned during instance creation, not the time zone of the underlying operating system. For SQL Database, the time zone is always set to UTC and CURRENT_TIMEZONE returns the name of the UTC time zone.

How do I convert UTC time to local time in SQL?

SELECT CONVERT(datetime, SWITCHOFFSET(CONVERT(DATETIMEOFFSET, GETUTCDATE()), DATENAME(TZOFFSET, SYSDATETIMEOFFSET()))) AS LOCAL_IST; Here, the GETUTCDATE() function can be used to get the current date and time UTC. Using this query the UTC gets converted to local IST.


1 Answers

I figured it out. In the SSRS report, I've added a reference to the assembly System.Core

Then anywhere I needed to convert the timezone I used:

=Code.FromUTC(Fields!UTCDateFromDatabase.Value, Parameters!TimeZone.Value)

where Parameters!TimeZone.Value is the string value of the timezone which can be retrieved in the application by using TimeZone.CurrentTimeZone.StandardName

I should probably put what FromUTC does:

Shared Function FromUTC(ByVal d As Date, ByVal tz As String) As Date
 Return (TimeZoneInfo.ConvertTimeBySystemTimeZoneId(d, TimeZoneInfo.Utc.Id, tz))
end function
like image 181
jamiegs Avatar answered Sep 18 '22 20:09

jamiegs