Hii,
Why we use NULLIF function?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Akash AhlawatRank #3,915 · 379 reputationPosted Nov 29, 2011, 8:50 PM EST
Example:SELECT Store_name, NULLIF(Actual,Goal) FROM Sales_Data;
Hemant KumarRank #1,202 · 1.5K reputationPosted Nov 29, 2011, 7:40 PM EST
It is the same as the following CASE statement:
SELECT CASE ("column_name")
WHEN "expression 1 = expression 2 " THEN "NULL"
[ELSE "expression 1"]
END
FROM "table_name"
For example, let's say we have a table that tracks actual sales and sales goal as below:
Table Sales_Data
Store_name Actual Goal
Store A 50 50
Store B 40 50
Store C 25 30
We want to show NULL if actual sales is equal to sales goal, and show actual sales if the two are different. To do this, we issue the following SQL statement:
SELECT Store_name, NULLIF(Actual,Goal) FROM Sales_Data;
The result is:
Store_name NULLIF(Actual,Goal)
Store A NULL
Store B 40
Store C 25
Pravin MoreRank #1,419 · 1.2K reputationPosted Nov 29, 2011, 7:42 PM EST
Hi Alec,
NULLIF function in sql server
1.Returns a null value if the two specified expressions are equal.
2.returns the first expression if the two expressions are not equal.
If the expressions are equal, NULLIF returns a null value of the type of the first expression
syntax:-
NULLIF ( expression , expression )
Thanks,
Pravin.
Satyapriya NayakRank #47 · 31.5K reputationPosted Nov 29, 2011, 9:11 PM EST
SQL NULLIF function takes 2 parameters: it returns NULL if the 2 parameters are equal, otherwise it returns the value of the first parameter.
SQL NULLIF Function Syntax
1. NULLIF(expression1, expression2)
NULLIF(expression1, expression2)
SQL NULLIF Function Example
Table: Users
UserId userName screenName
1 Liysheet Liy Sheet
2 Mohisy Mohisy
3 Nuoo Nuhoo
4 Mhquu Mhquu
5 Yuhiaa Uhiooo
Show NULL if userName is equal to screenName, otherwise show userName:
1. SELECT userId, NULLIF(userName, screenName)
2. FROM Users
SELECT userId, NULLIF(userName, screenName)
FROM Users
The output
UserId NULLIF(userName, screenName)
1 Liysheet
2 NULL
3 Nuoo
4 NULL
5 Yuhiaa
Thanks
Alec Stewart23 reputationPosted Nov 29, 2011, 11:08 PM EST