⟩ How To Replace Given Values with NULL using NULLIF()?
Sometime you want to hide certain values by replacing them with NULL values. SQL Server offers you a nice function called NULLIF() to do this:
NULLIF(expression, value)
-- Returns NULL if "expression" equals to value"
-- Returns "expression", otherwise
NULLIF() can be viewed as the reverse function of ISNULL(). The tutorial script below shows you a good example of using NULLIF():
USE GlobalGuideLineDatabase;
GO
SELECT id, counts FROM ggl_links;
GO
id counts----------- -----------
101 NULL
102 8
1101 NULL
202 NULL
2101 NULL
2102 NULL
301 NULL
302 NULL
-- converting NULL to 0
UPDATE ggl_links SET counts=ISNULL(counts,0);
GO