NULLIF

From SQLZOO
Revision as of 13:34, 16 July 2012 by Connor (Talk | contribs) (Created page with "<table align='right' border='1'> <caption>Compatibility</caption> <tr><th colspan='3'>NULLIF(f1,f2)</th></tr> <tr><td align='center'>'''Engine'''</td><td align='center'>'''OK'...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, 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