Is it possible to create a udf where the return type is the same as the first argument.
Other notes:
- The first argument can be any type and if it is null the return value is null with no underlying type.
- Constraints on the second argument so the type matches that of the first.
http://msdn.microsoft.com/en-us/library/aa933210(v=SQL.80).aspx
You could try using the sql_variant datatype for a UDF. (I’ve haven’t BTW)
However, a simple in-line ISNULL use the datatype of the 1st argument anyway.
Unless you haven’t given us full information in the question about what you intend to achieve…
Edit: after update
The whole point of ISNULL is to replace a NULL (1st argument) with a value (2nd argument).
There is no “type” of NULL. An int or a varchar can take a NULL value. But it’s still the base datatype
ISNULL constrains 2nd argument to the 1st type anyway: the 2nd argument must be implicitly convertible. So
SELECT ISNULL(CAST(NULL AS int), 'foo')will failSo, use in-line ISNULL which does what you want. A udf is not needed
Edit, after comment
You can’t have one udf to cover all datatypes.
Not least, a udf has a single return datatype.