The IS NOT NULL is used in SQL to find a non-NULL value. It returns TRUE if a non-NULL value is found, otherwise it returns FALSE.
SYNTAX
SELECT col1, col2, .. FROM Table WHERE Expression IS NOT NULL
expression is the expression to check for a NOT NULL value.
Lets take a look at IS NOT NULL in SQL , first we will create a sample table and insert some records into table.
CREATE TABLE dbo.EmpData ( EmpId INT, Name VARCHAR(100), Code VARCHAR(10), Department VARCHAR(50), Age INT ) INSERT INTO dbo.EmpData (EmpId, Name, Code, Department, Age) VALUES (1, 'Ajit Singh', null, 'CS', 35), (2, 'Mohit Sharma', 'EmXTY', null, 31), (3, 'Sujit Kumar', 'fMFSf', 'ES', 39), (4, 'Mukul Aggarwal', 'Hrtms', null, 41), (5, 'Sumit viyas', null, 'CS', 32), (6, 'Sanjay Rawat', null, 'ES', 64), (7, 'Gaurav Kumar', 'Ryrgt', null, 43), (8, 'Amit kumar', null, null, 28)
Now we have a table named EmpData as shown below.
SELECT * FROM dbo.EmpData
Lets see how to use IS NOT NULL to find the non-null values in table.
As you can see the output of table EmpData in above screenshot, there are few null values in Code and Department columns.
Lets select the employee records from table EmpData, whose Code is not null.
SELECT * FROM dbo.EmpData WHERE Code IS NOT NULL
You can see, it returns only those records whose code values is not null.
Lets see one more example, this time we will select all employees from EmpData table whose Code values, or Department name is not null.
SELECT * FROM dbo.EmpData WHERE Code IS NOT NULL OR Department IS NOT NULL
You can see, it returns only those employee’s records whose either code values is not null or department name is not null.
Lets select all employees from EmpData table whose Code values, and Department name is not null.
SELECT * FROM dbo.EmpData WHERE Code IS NOT NULL and Department IS NOT NULL
You can see, it returns only those employees whose code value and department name is not null.
Also Read..
SQL Server NOT NULL Constraint