“WHERE IS NULL, IS NOT NULL or NO WHERE clause depending on SQL Server parameter value”?
1. Where IS NULL?
2. Where IS NOT NULL?
3. Where ANY VALUE (NULL AND NOT NULL)?
A “NULL” is a special value that signifies “no value” and comparing a column to NULL using the (=) operator is undefined. Instead use WHERE IS NULL or WHERE IS NOT NULL.
Syntax:-
-- WITH IS NULL SELECT ColumnName FROM TableName WHERE ColumnName IS NULL -- WITH IS NOT NULL SELECT ColumnName FROM TableName WHERE ColumnName IS NOT NULL
Examples:-
--SELECT ALL COUNTRIES SELECT * FROM Countries
Result:-
Id CountryName CountryCode ----------------------------------- 1 India IN 2 Nepal NP 3 USA US 4 Rasia NULL 5 Australia AUS
Query With IS NULL:-
-- SELECT IS NULL SELECT ID, CountryName, CountryCode FROM Countries WHERE CountryCode IS NULL
Result:-
ID CountryName CountryCode ----------------------------------- 4 Rasia NULL
Query with IS NOT NULL:-
-- SELECT IS NOT NULL SELECT ID, CountryName, CountryCode FROM Countries WHERE CountryCode IS NOT NULL
Result:-
ID CountryName CountryCode ----------------------------------- 1 India IN 2 Nepal NP 3 USA US 5 Australia AUS
I hope you are enjoying with this post! Please share with you
friends. Thank you!!