I have a datetimeoffset column in a table with varying TZ offsets in the rows. I’d like to run them all through the ToLocalTime method on the BCL type System.DateTimeOffset (due to Daylight Saving, I can’t just use the SWITCHOFFSET function). The ‘clr enabled’ is already reconfigured.
The ‘brute force’ approach appears to be creating a static class with a static method, something like:
public static class DateTimeUtils
{
[SqlFunction]
public static DateTimeOffset ToLocalTimeZone(DateTimeOffset input)
{
return input.ToLocalTime();
}
}
Is there any simpler route that would allow calling the BCL type/method without having to create an assembly in the database just for this?
I was hoping I’d be able to just call a method off of the SQL type, but no such luck. Trying something like this:
DECLARE @foo datetimeoffset = getdate()
select [datetimeoffset]::ToLocalTime(@foo)
… gives
Msg 258, Level 15, State 1, Line 3
Cannot call methods on datetimeoffset.
Thanks!
No, there isn’t. I came to this conclusion after researching this a lot. Here is my solution to the same problem: https://stackoverflow.com/a/11063348/122718