NULLIF

From SQLZOO
Jump to navigation Jump to search
Compatibility
NULLIF(f1,f2)
EngineOKAlternative
ingresYes
mysqlYes
oracleYes
postgresYes
sqlserverYes

NULLIF

NULLIF returns NULL if the two arguments are equal; otherwise NULLIF returns the first argument.

   NULLIF(x,y) = NULL if x=y
   NULLIF(x,y) = x if x != y     

NULLIF can be used to replace a specific value with NULL. In this example the party Lab is replaced with NULL.

SELECT name, party
      ,NULLIF(party,'Lab') AS aff
  FROM gisq.msp WHERE name LIKE 'C%'
SELECT name, party
      ,NULLIF(party,'Lab') AS aff
  FROM msp WHERE name LIKE 'C%'

See also

Language:Project:Language policy English  • Deutsch
DataWars, Data Science Practice Projects - LogoDataWars: Practice Data Science/Analysis with +100 Real Life Projects