Blog Post

Inconsistent NullIF Behaviour ?

,

NullIf can be a very handy function sometimes.  For those who have not used it, if the result of the two parameters are equal then null is returned.  So if we execute :

Code Snippet
  1. Select nullif(10,10) as Result1
  2. Select nullif(10,9)  as Result2

 

We get the result of

image

For some test data, I required a column that was randomly null.  Using ABS(CHECKSUM(NEWID())) to generate the random value and then modulus 10, to get a random value between 0 and 9 , then passing that into NULLIF as one parameter and 10 as the other, I should of got random values between 1 and 9 with a selection of NULL values.  Here’s the code :

Code Snippet
  1. select top(1000) ROW_NUMBER() over (order by (select null)) as ID,
  2.        nullif(ABS(checksum(newid()))%10,0)  as RandomNonZero
  3. from sys.columns a

And here's the result…

image

Interesting , does that mean that NULLIF is not working ?  Plainly it is sometimes, after all there are some NULL values. 

Much like in my previous post detailing the differences between ISNULL and COALESCE,  the NULLIF function is expanded by the engine into a case statement as shown below.

 

image

So,  its working but the value outputted is not the value tested.

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating