The Wayback Machine - https://web.archive.org/web/20240619011040/https://www.geeksforgeeks.org/how-to-check-a-column-is-empty-or-null-in-sql-server/
Open In App

How to Check a Column is Empty or Null in SQL Server

Last Updated : 31 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In SQL Server table columns, there can be times when there is NULL data or Column Value is Empty (”). When data is displayed or used in data manipulations, there could be a need to remove these records with NULL values or Empty column values or replace the NULL value with another value like EMPTY value (”) to avoid NULL value errors. There could also need to find NULL column values or Empty column data to update the NULL values or Empty values. So there needs to be some way to identify NULL and Empty column values.

In this article let us discuss in detail, how to check if a column is Empty or NULL in SQL Server, with examples and different methods.

Checking if a Column is Empty or NULL

In a Table Column sometimes there can be no actual or valid data and it could be NULL or Empty (”) or some space value saved. There are many methods or functions to identify the NULL or Empty values. Below are some of them:

  1. IS NULL
  2. IS NOT NULL
  3. ISNULL() Function
  4. NOT ISNULL() Function
  5. DATALENGTH()
  6. LEN()

Methods for Identifying NULL or Empty Values in SQL Server

Explained below with examples, the above five methods.

Below is the Table ‘StudentsInfo‘ with data used for the examples in this article:

StudentsInfo-Table-Data

StudentsInfo table data

1. IS NULL

This key word returns any records with NULL value in the column specified in a Table.

Syntax:

WHERE {COLUMN NAME} IS NULL

Example:

Select * from StudentsInfo 
WHERE ContactPhone IS NULL

The above query checks for null values in the column ‘ContactPhone‘ from ‘StudenetsInfo‘ table. Below is the output.

Output:
IS-NULL---Keyword

IS NULL example output

In this example, the result includes rows where the “ContactPhone” column is NULL for various students in the “StudentsInfo” table. This query is useful for identifying records where specific contact information is missing.

2. IS NOT NULL

This key word returns any records without NULL value in the column specified.

Syntax:

WHERE {COLUMN NAME} IS NOT NULL

Example:

Select * from StudentsInfo 
WHERE ContactPhone IS NOT NULL

The above query checks for values without NULL in the column ‘ContactPhone‘ from ‘StudenetsInfo‘ table and returns actual values without NULL.

Output:

IS-NOT-NULL---Keyword

IS NOT NULL – Example

In the above sample output, we can see data without any NULL values, meaning records with NULL values are removed from the result output. It is to be noted that empty string value or column with spaces are displayed in the output if it is there and only NULL is filtered out. If we check the below query you can understand this as this column has EMPTY (”) and SPACES (‘ ‘)

Select * from StudentsInfo 
WHERE Remarks IS NOT NULL

Output:

RemarksColumn-IS-NOT-NULL

NOT NULL with Remarks column

3. ISNULL() Function

The function ISNULL() returns all records with ‘NULL’ values and records having Empty value (”) or Spaces (‘ ‘) in the specified column.

Syntax:

ISNULL(expression, replacement)

Expression can be a column name and replacement is the string value which will replace the column value.

Example:

SELECT * FROM StudentsInfo 
WHERE ISNULL(Remarks, '') = ''

In the above example ‘Select’ query, all the NULL, EMPTY, and SPACE values are returned as below:

Output:

ISNULL-Function

ISNULL Function example

4. NOT ISNULL() Function

The function NOT ISNULL() is just the opposite of ISNULL() Function explained above and returns all records without NULL, Empty, and Spaces in a particular column specified.

Syntax:

NOT ISNULL(expression, replacement)

Example:

SELECT * FROM StudentsInfo 
WHERE NOT ISNULL(Remarks, '') = ''

The above select statement with ‘NOT ISNULL()’ returns all records in EmployeesInfo table which has only valid data, meaning it does not show any records with NULL or Empty column or column with Spaces.

Output:

NOT-ISNULL-Function

NOT ISNULL Function Example

In the above output, we can records only with valid data as explained above.

5. DATALENGTH()

The DATALENGTH() function returns the actual length of the data or spaces in the specified column. We can use the DATALENGTH() function to check for records with an Empty column.

Syntax:

WHERE DATALENGTH({Expression})

the Expression can be a column name or a string expression

Example:

Select * from StudentsInfo 
WHERE DATALENGTH(Remarks)=0

Output:

DATaLENGTH-Function

Datelength function example

In the above output, we can see records with only EMPTY (”) value in the column ‘Remarks’. Datelength returns the actual length of data including Spaces. So the Datalenth(Remarks)=0 return only the records with column having 0 length, which can be only EMPTY value.

6. LEN()

The LEN() returns the actual length of the data in specified column. But it can used to check for records with Empty column and Spaces.

Syntax:

WHERE LEN({Expression})

The Expression can be a column name or a string expression

Example:

Select * from StudentsInfo 
where LEN(Remarks) = 0

Output:

LEN-Function

LEN Function example

The above Select query returns all columns with Empty (”) values and Spaces (‘ ‘) from the column Remarks in table StudentsInfo.

The above query is same as the below query:

Select * from StudentsInfo 
WHERE Remarks=''

This also returns the same result as the previous query using LEN() function.

Conclusion

In this article, we have discussed in detail about records with NULL, EMPTY(”), and SPACES (‘ ‘) values and how to filter records without these values. We have seen 6 types of keywords or Functions to filter records with or without NULL, EMPTY and SPACE values based on the query result required. Checking for NULL values is very important in many situations to avoid NULL value error. Whenever situation requires check for NULL value in a column before doing any data manipulations to avoid errors.



Similar Reads

How to Check GUID is Null or Empty in SQL Server
GUID stands for Globally Unique Identifier, the GUID is a 16-byte long binary data type. the GUID is special because it is globally unique across all the databases, tables, and servers available. In the GUID there is a unique reference number present which is generated by the algorithm every time we execute the query to generate GUID. the GUID is s
5 min read
How to Set a NOT NULL Column into NULL in PostgreSQL?
PostgreSQL is an open-source relational database management system in short RDBMS. It is commonly known for its reliability and vast feature set. We can clearly state that it is one of the most powerful RDBMS available. We often create some columns with NOT NULL constraints but later on, there will be some possibility that we have to add some NULL
4 min read
How to Set a Column Value to NULL in SQL Server
In the world of database management, SQL Server is a leading and extensively utilized system. A fundamental task within SQL Server is manipulating data within tables, and setting a column value to NULL is a common operation. Whether it's for maintaining data integrity, performing updates, or meeting specific business logic requirements, efficiently
4 min read
How to Check a Column is Empty or Null in MySQL?
In the databases, determining whether a column is empty or null is a common task. MySQL provides various techniques to perform this check, allowing users to filter and manipulate data efficiently. This article delves into the methods for checking if a column is empty or null in MySQL, outlining the syntax and presenting practical examples to illust
4 min read
How to Set a Column Value to Null in PL/SQL?
In PL/SQL, setting a column value to NULL is a common requirement when working with databases. Understanding how to accomplish this task is essential for database developers and administrators. In this article, we will delve into the concept of setting a column value to NULL in PL/SQL, covering the syntax, examples, and output explanations. PL/SQL:
4 min read
SQL Server NOT NULL Constraint
The SQL Server NOT NULL constraint is used to ensure that when we insert the rows to the table the column is never NULL. It throws an error when there is no value for a particular column when it is with a NOT NULL constraint. The primary key by default comes with the NOT NULL and UNIQUE constraints so the primary key can't be NULL at any time. The
5 min read
SQL Server NULL Values
In SQL Server, NULL represents the absence of a value in a column. It signifies missing or undefined data, distinct from zero or an empty string. SQL Server uses a three-valued logic, and handling NULL is crucial for accurate querying and reporting. Conditions like IS NULL and IS NOT NULL are used to check for NULL values, and functions like COALES
5 min read
How to Create Unique Constraint With NULL Columns in SQL Server
SQL Server is a versatile database developed by Microsoft and is one of the most used databases in the world. It provides the developer with a set of functionalities to create tables, insert data in them and then manipulate and play with them as and when necessary. In this article, we are going to see how we can create unique constraints with NULL
6 min read
How to Set a Column Value to NULL in SQLite?
SQLite is a lightweight and self-contained relational database management system in short RDBMS. Its has a server-less architecture which makes it a better option for small desktop and mobile applications. It also requires very low configuration which eventually helps the developer to integrate it into any applications with great ease. In this arti
4 min read
How to Set a Column Value to NULL in MariaDB
In MariaDB, the NULL represents an unknown value in a column. Changing a column value to NULL is the most common operation performed in MariaDB that allows us to remove existing data in a specific field. It is applicable in different ways including data correction, record inclusions and values setting. In this article, we will look through the diff
4 min read