Home > Net >  What is purpose of COALESCE = 0
What is purpose of COALESCE = 0

Time:10-11

There is some script that I seen where the code is as follows:

    AND (PC.SystemID = @SystemID OR COALESCE(@SystemID, 0) = 0)

I understand COALESCE chooses the first not null but not sure what the purpose would be for = 0.

CodePudding user response:

the where clause will return true for the whole AND

When

  • PC.SystemID = @SystemID
  • OR @SystemID is NULL
  • OR @SystemID = 0

so

@SystemID IS NULL OR @SystemID = 0 is equivalent to COALESCE(@SystemID, 0) = 0

  • Related