I rather think that
NULLIF(A, B)
is syntactic sugar for
CASE WHEN A = B THEN NULL ELSE A END
But you are correct: it is mere syntactic sugar to aid the human reader.
W3Schools
w3schools.com › sql › func_sqlserver_nullif.asp
SQL Server NULLIF() Function
SQL Examples SQL Editor SQL Quiz SQL Exercises SQL Server SQL Syllabus SQL Study Plan SQL Bootcamp SQL Certificate SQL Training ... The NULLIF() function returns NULL if two expressions are equal, otherwise it returns the first expression.
Videos
07:17
SQL NULLIF function: How to Use the NULLIF Function - YouTube
06:07
SQL SERVER||NULLIF Function in SQL and use case of NULLIF - YouTube
08:49
Using SQL NULLIF with Missing Values | Essential SQL - YouTube
53:00
SQL NULL Functions | COALESCE, ISNULL, NULLIF, IS (NOT) NULL | ...
04:59
Nullif function in sql server - YouTube
21. NULLIF() | SQL NULL FUNCTION | SQL Server Tutorial PART 21 ...
Top answer 1 of 5
9
I rather think that
NULLIF(A, B)
is syntactic sugar for
CASE WHEN A = B THEN NULL ELSE A END
But you are correct: it is mere syntactic sugar to aid the human reader.
2 of 5
8
I often use it where I need to avoid the Division by Zero exception:
SELECT
COALESCE(Expression1 / NULLIF(Expression2, 0), 0) AS Result
FROM …
Snowflake Documentation
docs.snowflake.com › en › sql-reference › functions › nullif
NULLIF | Snowflake Documentation
SELECT a, b, NULLIF(a,b) FROM i; --------+--------+-------------+ a | b | nullif(a,b) | --------+--------+-------------+ 0 | 0 | [NULL] | 0 | 1 | 0 | 0 | [NULL] | 0 | 1 | 0 | 1 | 1 | 1 | [NULL] | 1 | [NULL] | 1 | [NULL] | 0 | [NULL] | [NULL] | 1 | [NULL] | [NULL] | [NULL] | [NULL] | --------+--------+-------------+
Oracle
docs.oracle.com › en › database › oracle › oracle-database › 26 › sqlrf › NULLIF.html
SQL Language Reference
1 month ago - NULLIF compares expr1 and expr2. If they are equal, then the function returns null. If they are not equal, then the function returns expr1.
W3Schools
w3schools.com › sql › sql_isnull.asp
W3Schools.com
String Functions: ASCII CHAR_LENGTH ... CAST COALESCE CONNECTION_ID CONV CONVERT CURRENT_USER DATABASE IF IFNULL ISNULL LAST_INSERT_ID NULLIF SESSION_USER SYSTEM_USER USER VERSION SQL Server Functions...
IBM
ibm.com › docs › en › db2-for-zos › 12.0.0
NULLIF scalar function - Db2 SQL
The NULLIF function returns the null value if the two arguments are equal; otherwise, it returns the value of the first argument.
W3Schools
w3schools.com › sql › func_mysql_nullif.asp
MySQL NULLIF() Function
SQL Examples SQL Editor SQL Quiz SQL Exercises SQL Server SQL Syllabus SQL Study Plan SQL Bootcamp SQL Certificate SQL Training ... The NULLIF() function compares two expressions and returns NULL if they are equal.
AWS
docs.aws.amazon.com › amazon redshift › database developer guide › sql reference › sql functions reference › conditional expressions › nullif function
NULLIF function - Amazon Redshift
The NULLIF expression compares two arguments and returns null if the arguments are equal. If they are not equal, the first argument is returned.
MSSQLTips
mssqltips.com › home › sql coalesce, isnull, nullif in sql server, oracle and postgresql
SQL COALESCE, ISNULL, NULLIF in SQL Server, Oracle and PostgreSQL
March 21, 2022 - There is another expression/function present in all three RDBMS that is used with NULL and that is NULLIF(). This last function has two arguments and returns NULL if both arguments are equal, otherwise it returns the first argument.
SQLServerCentral
sqlservercentral.com › forums › topic › nullif-and-0
NULLIF and 0 – SQLServerCentral Forums
March 25, 2021 - The NULLIF function returns NULL if the 2 arguments are equal, otherwise it just returns the first argument. The first and third expressions are clearly equivalent expressions, so return NULL.
TechOnTheNet
techonthenet.com › sql_server › functions › nullif.php
SQL Server: NULLIF Function
In SQL Server (Transact-SQL), the NULLIF function compares expression1 and expression2. If expression1 and expression2 are equal, the NULLIF function returns NULL.
Reddit
reddit.com › r/sql › can someone give me a real world example of the nullif function? i know that it’s supposed to return a bull if the two expressions are equal but i can’t think of a real world scenario where i would use this function.
r/SQL on Reddit: Can someone give me a real world example of the NULLIF function? I know that it’s supposed to return a Bull if the two expressions are equal but I can’t think of a real world scenario where I would use this function.
January 10, 2022 -
Thanks!
Meant Null* not Bull. Apologies.
Top answer 1 of 5
11
This is the main "real world" use: select 1/nullif(foo,0) as foo_inverse Wrapping your divisors in nullif keeps your query from erroring out due to divde by zero. If you want some default value (say X) instead of null as a result, then wrap the whole thing in a nvl(). select nvl(1/nullif(foo,0),X) as foo_inverse
2 of 5
8
I use it a lot because the software I work with typically does not set values to NULL when a user removes a value from a record. It will often set the value to an empty string which I then have to treat the same way as a NULL. Most of my NULL checks look like COALESCE(NULLIF(foo, ''),bar).