Where and how to use the NULLIF (X, Y) SQLITE function?

I know that the SQLULL function NULLIF (X, Y) is equivalent to:

CASE
    WHEN
        X = Y
    THEN
        NULL
    ELSE
        X
END

and the IFNULL (X, Y) function works equivalently:

CASE
    WHEN
        X IS NULL
    THEN
        Y
    ELSE
        X
END

The IFNULL (X, Y) SQLITE function is used to replace the NULL X values ​​with Y, but I cannot understand the use of the SQLITE NULLIF (X, Y) function. Please explain with examples, so this is more useful.

+4
source share
1 answer

The IFNULL function is used when the database contains NULL values, but you want to treat these values ​​as something else; eg:

SELECT Name, IFNULL(Age, 'unknown') AS Age FROM People

NULLIF , , NULL, NULL. . , , , :

SELECT COUNT(NULLIF(Bonus, 0)) FROM Employees

, :

SELECT COUNT(*) FROM Employees WHERE Bonus != 0

NULLIF , IFNULL.

+12

All Articles