I need to create a user defined function that would operate similarly to ISNULL system function in the respect that it would accept arguments of any type and would return the value of the same type. How is this done?
Using SQL_VARIANT as a datatype would not work as SQL_VARIANT values require explicit casting. ISNULL does the data type conversions implicitly.
How to declare the generic types that do not require explicit casting?
Let me make it clear. I am not looking to replicate the functionality of ISNULL function. I used ISNULL function as a pattern of a function that takes two arguments of any data type supported by SQL Server and returns the value of the same data type as the arguments.
There are other SQL Server functions that implement the same pattern: accept arguments which data type is not explicitly declared and to return the value of some other data type, which is also not explicitly declared at function definition. The examples are: NULLIF, CAST, CONVERT.
I am wondering how this pattern could be accomplished, because any UDF requires explicit definition of arguments and return value data types.