How to Check Equality Between Two Column When Values Could Be Null

Josh Cash 25 Reputation points
2023-12-05T17:37:02.53+00:00

In Azure Data Factory, I have two column values which I am trying assert if they are different so I know to update the source with a new value. The issue is that either value might be null as well and that breaks the comparison logic.

Value1 != Value2 or notEquals(Value1, Value2) both will return NULL instead of a true or false if one or both values is NULL. This is contradictorily of what I would expect.

I would expect:

Value1 = NULL, Value2 = 2.3 to result in true instead of NULL

Am I missing something or do I have to do null checks and then the comparison checks?

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,639 questions
0 comments No comments
{count} votes

Accepted answer
  1. Subashri Vasudevan 11,231 Reputation points
    2023-12-06T15:36:42.9033333+00:00

    Hi,

    Since both can be null and I didn't want to convert each number to some default value which could be a valid value, I did the following:

    iif(isNull(Value1 != Value2), !isNull(Value1) || !isNull(Value2), Value1 != Value2)
    

    While I am doing the equality check twice in some cases, it makes sure that I don't have to treat a valid value as a default and thus cause false negatives.

    Thanks

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Josh Cash 25 Reputation points
    2023-12-06T14:49:54.5666667+00:00

    I ended up doing this:

    Since both can be null and I didn't want to convert each number to some default value which could be a valid value, I did the following:

    iif(isNull(Value1 != Value2), !isNull(Value1) || !isNull(Value2), Value1 != Value2)

    While I am doing the equality check twice in some cases, it makes sure that I don't have to treat a valid value as a default and thus cause false negatives.

    1 person found this answer helpful.

  2. Subashri Vasudevan 11,231 Reputation points
    2023-12-06T02:27:21.5466667+00:00

    Hi,

    You can use iif() function and isNull() function to achieve your requirement.( And I assume it is within a data flow).

    iif(isNull(value1),toDecimal(0.00),toDecimal(value1))

    And once you do this check and assign a default value to value1, you can compare value1 and value2 using equality operator.

    Hope that resolves your issue.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.