NULLIF function in SQL Server

The NULLIF function in SQL Server returns a NULL value if the two input expressions return the same value else the first value (expression) is returned.

For example , assume that you want to compare two columns (Address1 and Address2) in the Address table and want to return NULL if both are same to indicate there is NO change and if the addresses are different to use the first one , we could use the NULLIF in the query.

Use AdventureWorks2014
GO
SELECT  AddressId,AddressLine1,AddressLine2,
      NULLIF([AddressLine1],[AddressLine2])   
  FROM [AdventureWorks2014].[Person].[Address]
  ORDER BY AddressID 
image
%d