Hy,
I have post a question about CLR User-Defined Aggregates few month ago oon this post.
This works like a charm. But now I would like to quite the same functi
When using SQL_VARIANT
/ object
, you can determine the type by using the GetType()
method as follows:
[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = false, IsPrecise = true)]
public static SqlBoolean GetIfSmallInt(object SomeValue)
{
return (SomeValue.GetType() == typeof(SqlInt16));
}
And test it using:
DECLARE @DateTime DATETIME = GETDATE();
SELECT dbo.GetIfSmallInt(@DateTime);
-- 0
DECLARE @SmallInt SMALLINT = 5;
SELECT dbo.GetIfSmallInt(@SmallInt);
-- 1
Please keep in mind that using SQL_VARIANT
/ object
has a noticeable performance penalty. Only use it when you absolutely need to. If you just need to pass in INT / SMALLINT / BIGINT, then use BIGINT
/ SqlInt64
as the input parameter type.