3

I am aware of the NULLIF function which compares two values and returns NULL if they match.

Is there a function which will compare two values and return NULL if they don’t match? That is filter out the other values.

I know I can do something like that using CASE:

CASE column WHEN value THEN 1 END

I also know that I can write a function.

Perhaps there is a subtle trick to using NULLIF that I am unaware of.

I have listed a few versions of SQL in the tags, but I think NULLIF is generic. I am looking for a solution which is also generic, so that it can be applied to any standard database.

6

NULLIF(expr1, NULLIF(expr1, expr2)) does what you want.

  • 1
    If both expressions are NULL this will return NULL. That may be acceptable, depending on how you define "match"! – Michael Green Oct 27 '18 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.