An extension to @db2's answer with less (read:zero) hand-wrangling:

DECLARE @tb nvarchar(512) = N'dbo.[table]';

DECLARE @sql nvarchar(max) = N'SELECT * FROM ' + @tb
    + N' WHERE 1 = 0';

SELECT @sql += N' OR ' + QUOTENAME(name) + N' IS NULL'
    FROM sys.columns 
    WHERE [object_id] = OBJECT_ID(@tb)
    AND is_nullable = 1;

EXEC sys.sp_executesql @sql;
Answer from Aaron Bertrand on Stack Exchange
🌐
W3Schools
w3schools.com › sql › sql_null_values.asp
SQL NULL Values - IS NULL and IS NOT NULL
SELECT column_names FROM table_name WHERE column_name IS NOT NULL; Below is a selection from the Customers table used in the examples: The IS NULL operator is used to test for empty values (NULL values).
🌐
Reddit
reddit.com › r/sqlserver › counting rows where all columns are either null or empty in the row?
r/SQLServer on Reddit: Counting rows where ALL columns are either null or empty in the row?
September 26, 2024 -

I'd rather not write a bunch of AND clauses, so is there a quick, efficient way to do this?

I'm importing some data with 10 fields into a SQL Server from a CSV file. Occasionally this file has null/empty values across all the cells/columns.

What I'd like to do is just write one relatively short sql statement to simply count (at first) all these rows. I'd rather do it without doing something like:

...and (column1 is null or column1 = '')
...and (column2 is null or column2 = '')

etc...

Is there a good way to do this, or am I stuck with the above?

🌐
Quora
codinghub.quora.com › How-to-check-if-multiple-columns-are-not-null-in-SQL
How to check if multiple columns are not null in SQL - Programming & Coding Hub - Quora
Answer: The most correct - if most tedious - way is to simply do: [code]SELECT * FROM sometab WHERE (rest of the whereclause) AND ( col1 IS NOT NULL or col2 IS NOT NULL or col3 IS NOT NULL or col IS NOT NULL ); [/code]A way that SQL newbies ...
🌐
C# Corner
c-sharpcorner.com › blogs › sql-server-isnull-with-multi-column-names
SQL Server ISNULL() With Multi Column Names
November 26, 2018 - See the following example of using ... function The IsNull function can check only if one value is null. It cannot check null for multiple values....
🌐
Sqlservercurry
sqlservercurry.com › 2016 › 06 › sql-server-isnull-with-multi-column_18.html
SQL Server ISNULL() With Multi Column Names
See the following example of using SQL Server ISNULL in a Select Statement: select empid, ename, IsNull(Passport_Number, 'Not Found') as 'Passport Status' from identification · Image2-IsNull-With-Single-Column Limitation of IsNull() function: IsNull function can check only if one value is null. It cannot check null for multiple values.
🌐
TutorialsPoint
tutorialspoint.com › select-not-null-column-from-two-columns-in-mysql
SELECT not null column from two columns in MySQL?
SELECT IF (yourColumnName1 ISNULL,yourColumnName2,yourColumnName1) AS NotNULLValue FROM SelectNotNullColumnsDemo; To understand the above syntax, let us create a table. The query to create a table is as follows: mysql> create table SelectNotNullColumnsDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, ...
Find elsewhere
🌐
YouTube
youtube.com › watch
How to find null or empty in SQL Server - YouTube
sql check if column is null or emptyhow to check if multiple columns are null in sqlisnull sql serversql is null or emptyfind null in sql serverhow to use nu...
Published   September 8, 2022
🌐
Coginiti
coginiti.co › home › analysis › compare values when one is null
How to Compare Two Values in SQL When One Is Null - Coginiti
February 1, 2024 - SELECT * FROM your_table WHERE column1 = column2 OR (column1 IS NULL AND column2 IS NULL); In this example, the SQL statement checks if column1 is equal to column2 or both columns are null.
🌐
SQLServerCentral
sqlservercentral.com › forums › topic › multiple-columns-search-with-some-blanknull-values
Multiple columns search with some blank/null values – SQLServerCentral Forums
September 9, 2012 - I have used dynamic SQL query.... because there is written that there is no fear of Sql injection because Query is still using parameters. Fot this time i have implemented it for Update query... will use for search later... but concept is clear... ... Don't know if it will help, but here is a script I wrote that loops through all tables, and creates the script to look for a particular string in any column of the listed data types.
Top answer
1 of 4
5

Two Solutions (Column is All NULLs, Column Contains Some NULLs)

I have slightly altered your original example in order to provide two solutions:

Column_1 Column_2 Column_3
-------- -------- --------
1        2        NULL
1        NULL     NULL
5        6        NULL

First, test for NULLs and count them:

select 
    sum(case when Column_1 is null then 1 else 0 end) as Column_1, 
    sum(case when Column_2 is null then 1 else 0 end) as Column_2, 
    sum(case when Column_3 is null then 1 else 0 end) as Column_3,
from TestTable 

Yields a count of NULLs:

Column_1  Column_2  Column_3
0         1         3

Where the result is 0, there are no NULLs.

Second, let's count the non-NULLs:

select 
    sum(case when Column_1 is null then 0 else 1 end) as Column_1, 
    sum(case when Column_2 is null then 0 else 1 end) as Column_2, 
    sum(case when Column_3 is null then 0 else 1 end) as Column_3,
from TestTable

...But because we're counting non-NULLs here, this can be simplified to:

select 
    count(Column_1) as Column_1, 
    count(Column_2) as Column_2, 
    count(Column_3) as Column_3,
from TestTable

Either one yields:

Column_1  Column_2  Column_3
3         2         0

Where the result is 0, the column is entirely made up of NULLs.

If you only need to check a given column, then TOP 1 is quicker because it should stop at the first hit:

select count(*) from (select top 1 'There is at least one NULL' AS note from TestTable where Column_3 is NULL) a

0 = There are no NULLs, 1 = There is at least one NULL

SELECT COUNT(*) FROM (SELECT TOP 1 'There is at least one non-NULL' AS note FROM sat_data_active_season_group WHERE season_group IS NOT NULL) a

0 = They are all NULL, 1 = There is at least one non-NULL

I hope this helps.

2 of 4
3

we can check with the help of IN like

...WHERE NULL IN (Column_2, Column_3)

from your comment Well the multiple column will be Column_3, Column_2 in format might be this is helpful for you

select * from (select Column_3, Column_2 from @temp where null in (Column_3, Column_2)) as Result
🌐
Reddit
reddit.com › r › SQL › comments › e2z7yh › is_there_a_way_to_select_when_multiple_columns
Is there a way to Select when multiple columns are not Null ...
May 5, 2019 - I need to query a large table and I want to select only rows where none of like 40+ columns are Null or Zero · Is there a way to do that without just making it a bunch of AND or OR statements? It's millions of records, so i want something that will run decently optimized
Top answer
1 of 6
20

How about:

SELECT TOP 3 *
FROM (SELECT DISTINCT 
        CASE WHEN B IS NULL THEN NULL ELSE 'foo' END AS B
        , CASE WHEN C IS NULL THEN NULL ELSE 'bar' END AS C
  FROM T 
  WHERE 
    (B IS NULL AND C IS NOT NULL) 
    OR (B IS NOT NULL AND C IS NULL) 
    OR (B IS NULL AND C IS NULL)
) AS DT
2 of 6
6

As I understand the question, you want to know whether a null exists in any of the columns values as opposed to actually returning the rows in which either B or C is null. If that is the case, then why not:

Select Top 1 'B as nulls' As Col
From T
Where T.B Is Null
Union All
Select Top 1 'C as nulls'
From T
Where T.C Is Null

On my test rig with SQL 2008 R2 and one million rows, I got the following results in ms from the Client Statistics tab:

Kejser                          2907,2875,2829,3576,3103
ypercube                        2454,1738,1743,1765,2305
OP single aggregate solution    (stopped after 120,000 ms) Wouldn't even finish
My solution                     1619,1564,1665,1675,1674

If you add the nolock hint, the results are even faster:

Select Top 1 'B as nulls' As Col
From T With(Nolock)
Where T.B Is Null
Union All
Select Top 1 'C as nulls'
From T With(Nolock)
Where T.C Is Null

My solution (with nolock)       42,70,94,138,120

For reference I used Red-gate's SQL Generator to generate the data. Out of my one million rows, 9,886 rows had a null B value and 10,019 had a null C value.

In this series of tests, every row in column B has a value:

Kejser                          245200  Scan count 1, logical reads 367259, physical reads 858, read-ahead reads 367278
                                250540  Scan count 1, logical reads 367259, physical reads 860, read-ahead reads 367280

ypercube(1)                     249137  Scan count 2, logical reads 367276, physical reads 850, read-ahead reads 367278
                                248276  Scan count 2, logical reads 367276, physical reads 869, read-ahead reads 368765

My solution                     250348  Scan count 2, logical reads 367276, physical reads 858, read-ahead reads 367278
                                250327  Scan count 2, logical reads 367276, physical reads 854, read-ahead reads 367278

Before each test (both sets) I ran CHECKPOINT and DBCC DROPCLEANBUFFERS.

Here are the results when there are no nulls in the table. Note that the 2 exists solution provided by ypercube are nearly identical to mine in terms of reads and execution time. I (we) believe this is due to advantages of Enterprise/Developer edition having use of Advanced Scanning. If you were using only the Standard edition or lower, Kejser's solution may very well be the fastest solution.

Kejser                          248875  Scan count 1, logical reads 367259, physical reads 860, read-ahead reads 367290

ypercube(1)                     243349  Scan count 2, logical reads 367265, physical reads 851, read-ahead reads 367278
                                242729  Scan count 2, logical reads 367265, physical reads 858, read-ahead reads 367276
                                242531  Scan count 2, logical reads 367265, physical reads 855, read-ahead reads 367278

My solution                     243094  Scan count 2, logical reads 367265, physical reads 857, read-ahead reads 367278
                                243444  Scan count 2, logical reads 367265, physical reads 857, read-ahead reads 367278
🌐
Alteryx Community
community.alteryx.com › t5 › Alteryx-Designer-Desktop-Discussions › Solve-for-isNull-across-multiple-columns-at-once › td-p › 764525
Solved: Solve for isNull across multiple columns at once - Alteryx Community
July 31, 2023 - Do these columns (and will they always) contain only numeric values. If so then data cleansing works perfectly, if not then you'd need to go down the route of the multi-field formula tool as the data cleansing tool would convert null values to blanks in columns that have non-numeric data types