T-SQL Tutorial

SQL Check if column is not null or empty


Check if column is not null

Before you drop a column from a table or before modify the values of an entire column, you should check if the column is empty or not.

Example

select count(*)
from Certifications
where price is not null;





Check if column is not null or empty

USE model;
GO
DECLARE @x_count int;
SET @x_count=0;
select @x_count = count(*) from Certifications where price is not null;
IF @x_count > 0
    BEGIN
       PRINT 'Column is not empty'
    END;
ELSE
    BEGIN
       PRINT 'Empty column'
    END;
GO

Result:

Column is not empty