Create SQL Server CLR Function (Date Time Conversion) UTC to PDST
sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
sp_configure 'clr enabled', 1
GO
RECONFIGURE
using System;
using System.Data;using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public partial class UserDefinedFunctions{
[Microsoft.SqlServer.Server.SqlFunction]public static SqlString convert_timezone(DateTime datetime, String source_time_zone = "UTC", String destination_time_zone = "Pacific Standard Time")
{ //set defaults return new SqlString(TimeZoneInfo.ConvertTime(datetime, TimeZoneInfo.FindSystemTimeZoneById(source_time_zone), TimeZoneInfo.FindSystemTimeZoneById(destination_time_zone)).ToString());
}
}
Msg 6522, Level 16, State 2, Line 3
A .NET Framework error occurred during execution of user-defined routine or aggregate "convert_timezone":
System.Security.HostProtectionException: Attempted to perform an operation that was forbidden by the CLR host.
The protected resources (only available with full trust) were: All
The demanded resources were: MayLeakOnAbort
Create SQL Server CLR Function (Date Time Conversion) UTC to PDST
In this guide we create a CLR Function for SQL Server 2012. The code should also work for SQL 2K8.