How to Check a Column is Empty or Null in SQL Server
Last Updated :
31 Jan, 2024
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:
- IS NULL
- IS NOT NULL
- ISNULL() Function
- NOT ISNULL() Function
- DATALENGTH()
- 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 data1. 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 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 - ExampleIn 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:
NOT NULL with Remarks column3. 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 example4. 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 ExampleIn 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:
Datelength function exampleIn 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 exampleThe 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 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
4 min read
How to Check if a Column Exists in a SQL Server Table?
In this article, we will look at how to check if a particular column exists in a database table or not. For checking the existence of a column we need to create the table first. So, let us create a table with some columns and data. Creating table: Syntax: CREATE TABLE table_name ( column1 datatype,
2 min read
How to Alter a Column from Null to Not Null in SQL Server
In SQL Server, columns are defined with specific constraints, one of which is the nullability of the column whether or not it can hold NULL values. As a database evolves, this setting may need to be changed particularly to ensure that certain data fields are always populated. Altering a column from
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
4 min read
How to Filter Rows Without Null in a Column in SQL?
Here we will see, how to filter rows without null in a column of an MS SQL Server's database table with the help of a SQL query using IS NOT NULL operator. For the purpose of demonstration, we will be creating a demo_orders table in a database called âgeeksâ. Creating the Database: Use the below SQL
2 min read
How to Update a Column in a Table in SQL Server
In the database management area, updating columns in a table is a normal query and it is important software that ensures the accuracy and integrity of data. Whether we are making spelling corrections, editing or altering existing information, or being attentive to changing requirements, carrying out
4 min read
How to Check Column Types in PostgreSQL?
In PostgreSQL, checking column types is an important aspect of understanding the structure and data stored in a database table. It helps database developers and administrators work effectively by providing a clear picture of the database schema. In this article, we will explore various methods to ch
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 set column values to NULL is essential for database developers and administrators. In this article, we will look into the concept of setting a column value to NULL in PL/SQL, covering
4 min read
How to Set a Column Value to Null in SQL?
You can set a column value to NULL using the SQL UPDATE statement. Through the UPDATE statement, existing records in a table can be changed. The fundamental syntax to set a column value to NULL is as follows. Syntax: UPDATE table_name set column_name=NULL WHERE Conditions; table_name: The name of th
2 min read
How to Check Whether a Variable is Empty in PHP?
Given some values of variables, the task is to check whether the variable is empty or not in PHP. An empty variable can refer to a variable that has not been set, a variable that has been explicitly set to an empty value, or a variable that contains a value that is considered empty. In this article,
5 min read