Solved

search SQL table for null in each column

Posted on 2014-03-31
6
576 Views
Last Modified: 2014-03-31
Hi Experts,

is there an easy(ish) way to run a select statment to run across each colum to see if there are null values?

thanks

Simon
0
Comment
Question by:SimonPrice33
  • 2
  • 2
  • 2
6 Comments
 
LVL 9

Accepted Solution

by:
edtechdba earned 500 total points
ID: 39966324
It sounds like checking to see which columns can be nullable first may help, here's an example below.
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMS
WHERE TABLE_SCHEMA = 'dbo'
  AND TABLE_NAME = 'YourTable'
  AND IS_NULLABLE = 'YES'

Open in new window


Then take your column names from the query above and add them to the WHERE clause below.

SELECT *
FROM YourTable
WHERE (col1 IS NULL
  OR col2 IS NULL
  OR col3 IS NULL)
  -- and so on until you've included all columns that are nullable

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39966337
do you mean NULL in all columns for a single row?
I agree there is nothing "simple", the only way is the one above, as you have to consider that for columns that cannot be null there will be some "default" value (at least).
you might want to consider such "default values" to be like a NULL value for your consideration...
0
 

Author Closing Comment

by:SimonPrice33
ID: 39966374
Im not checking to see if they are nullable, i was hoping that i wouldnt have to specify each column but thought that would be the only way.

thanks

simon
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 9

Expert Comment

by:edtechdba
ID: 39966401
If there are columns that are not nullable in your table, then you'll reduce the # of columns when searching for NULL values if you run first query before checking for NULL values.

Just a thought. Thank you.
0
 

Author Comment

by:SimonPrice33
ID: 39966411
the genuis who put this together made all fields nullable...
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39966526
sorry, you will have to bite the nail here...
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now