title: SQL Server User Defined Function taking arguments of any type like ISNULL
body: <p>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?</p>
  
  <p>Using SQL_VARIANT as a datatype would not work as SQL_VARIANT values require explicit casting.  ISNULL does the data type conversions implicitly.  </p>
  
  <p>How to declare the generic types that do not require explicit casting?</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>I am wondering how this pattern could be accomplished, because any UDF requires explicit definition of arguments and return value data types.</p>