Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andrew Gould
In this tutorial
In SQL Server when a value has not been entered in a column for a particular record it is referred to as NULL. There is a unique syntax for writing criteria to find these null entries.
You can spot nulls in a set of results quite easily!
You can find records which contain nulls by asking for those rows where a particular field IS NULL:
SELECT
FilmName
,FilmBoxOfficeDollars
FROM
tblFilm
WHERE
FilmBoxOfficeDollars IS NULL
Simple! The results of this query are shown below:
These films have no information in the FilmBoxOfficeDollars field.
Testing if a field is not null uses a syntax which is almost as simple as the one shown above:
SELECT
FilmName
,FilmBoxOfficeDollars
FROM
tblFilm
WHERE
FilmBoxOfficeDollars IS NOT NULL
The above query finds all of the records which have a value in the FilmBoxOfficeDollars field.
All of these records have some sort of value in the box office column.
Occasionally, people who are new to SQL will try to use the equals sign when testing for null, as in the example shown below:
SELECT
FilmName
,FilmBoxOfficeDollars
FROM
tblFilm
WHERE
FilmBoxOfficeDollars = NULL
With SQL Server's default settings this query won't return any results, even if there are nulls in the FilmBoxOfficeDollars field. It is possible to change this behaviour, however, by adding an extra line to the top of your query:
--Change the behaviour of NULL
SET ANSI_NULLS OFF
SELECT
FilmName
,FilmBoxOfficeDollars
FROM
tblFilm
WHERE
FilmBoxOfficeDollars = NULL
The query will now successfully find records with null in the FilmBoxOfficeDollars field, as shown below:
The query now finds nulls in the specified field.
It's difficult to think of a good reason for changing the standard behaviour of nulls in criteria. In fact, according to the Microsoft website, future versions of SQL Server will no longer allow you to do this.
That's everything on basic criteria in SQL queries!
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.