MOCKSTACKS
EN
Questions And Answers

More Tutorials









SQLServer NULL

Filtering for NULL in queries


The syntax for filtering for NULL (i.e. the absence of a value) in WHERE blocks is slightly different than filtering for specific values.

SELECT * FROM Employees WHERE ManagerId IS NULL ;
SELECT * FROM Employees WHERE ManagerId IS NOT NULL ;

Note that because NULL is not equal to anything, not even to itself, using equality operators = NULL or <> NULL (or != NULL) will always yield the truth value of UNKNOWN which will be rejected by WHERE.

WHERE filters all rows that the condition is FALSE or UKNOWN and keeps only rows that the condition is TRUE.

Nullable columns in tables


When creating tables it is possible to declare a column as nullable or non-nullable.

CREATE TABLE MyTable
(
 MyCol1 INT NOT NULL, -- non-nullable
 MyCol2 INT NULL -- nullable
) ;

By default every column (except those in primary key constraint) is nullable unless we explicitly set NOT NULL constraint.

Attempting to assign NULL to a non-nullable column will result in an error.

INSERT INTO MyTable (MyCol1, MyCol2) VALUES (1, NULL) ; -- works fine
INSERT INTO MyTable (MyCol1, MyCol2) VALUES (NULL, 2) ;
 -- cannot insert
 -- the value NULL into column 'MyCol1', table 'MyTable';
 -- column does not allow nulls. INSERT fails.


Conclusion

In this page (written and validated by ) you learned about SQLServer NULL . What's Next? If you are interested in completing SQLServer tutorial, your next topic will be learning about: SQLServer Example Databases and Tables.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.