Using SQL Server 2008, Visual Studio 2005, .net 2.0 SP2 (support for new SQL Server 2008 data types).
I am trying to write a SQLCLR function that takes DateTime2 as input and returns another DateTime2. eg
using System;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
namespace MyCompany.SQLCLR
{
public class DateTimeHelpCLR
{
[SqlFunction(DataAccess = DataAccessKind.None)]
public static SqlDateTime UTCToLocalDT(SqlDateTime val)
{
if (val.IsNull)
return SqlDateTime.Null;
TimeZone tz = System.TimeZone.CurrentTimeZone;
DateTime res = tz.ToLocalTime(val.Value);
return new SqlDateTime(res);
}
}
}
Now the above compiles in order. I want these SqlDateTimes to appear in SQL Server DateTime2, so I am trying to run this T-SQL:
CREATE function hubg.f_UTCToLocalDT
(
@dt DATETIME2
)
returns DATETIME2
AS
EXTERNAL NAME [SQLCLR].[MyCompany.SQLCLR.DateTimeHelpCLR].UTCToLocalDT
GO
This results in the following error:
Msg 6551, Level 16, State 2, Procedure f_UTCToLocalDT, Line 1 CREATE FUNCTION for "f_UTCToLocalDT" failed because the T-SQL and CLR types for the return value do not match.
DATETIME ( DATETIME2) . DATETIME2 . - , DateTime2 () SQLCLR?