1

Is it possible to implement user-defined function in SQL Server that has parameter expression and return type is same as expression?

Similar to https://learn.microsoft.com/en-us/sql/t-sql/language-elements/nullif-transact-sql.

Basically, I'm looking to implement extended version of NULLIF function.

2
  • You can use sql_variant, but otherwise I don't think you can have dynamic typing. Commented Dec 3, 2017 at 12:29
  • 1
    And sql_variant often doesn't really help as you need to cast it to the base type if you want to perform any further operations on it. Commented Dec 3, 2017 at 12:43

1 Answer 1

1

No.

This is a capability that only built in functions have.

The grammar for creating scalar functions requires

RETURNS return_data_type 

Which means you have to pick a specific return data type at design time. You can't have it inferred from the datatype passed in.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.