Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

search SQL table for null in each column

Posted on 2014-03-31
6
Medium Priority
?
615 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 1000 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 143

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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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 143

Expert Comment

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

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

916 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