Solved

query all fields that are completely NULL

Posted on 2015-01-23
5
121 Views
Last Modified: 2015-01-23
Hi Folks,
I have a table with a few hundred fields. Is there a way to drop all fields that have NULL values?
0
Comment
Question by:mrosier
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40566785
Define 'drop fields'.  Delete the row?  Delete the column?
0
 

Author Comment

by:mrosier
ID: 40566788
delete the columns which contain all NULL values. Or as it turns out in the case of this data where = 'NULL'
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 40566949
before you get to the stage of actually removing data from your system...

can you tell us why you feel it necessary to do this?

have you considered just creating a view over the table which ignores the "empty" columns for your current requirement?
0
 

Author Comment

by:mrosier
ID: 40566957
taking up space when trying to look at the table. I mean if I can generate a view that ignores all columns that = 'NULL' (because that is how the table is working with it), and make it a table, that would be perfect
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40567023
Script below will gen SQL to give you the NULL column counts for the table name specified.  You can then just check that table to determine which cols have NULL totals that equal the total rows count.

DECLARE @table_name varchar(200)

SET @table_name = 'dbo.<your_table_name_goes_here>' --<--

DECLARE @sql varchar(max)

SET @sql = ''
SELECT @sql = (
    SELECT CHAR(10) + REPLACE(',SUM(CASE WHEN [$column$] IS NULL THEN 1 ELSE 0 END) AS [$column$_null_count]',
        '$column$', CAST(c.name AS varchar(128)))
    FROM sys.columns c
    WHERE
        c.object_id = OBJECT_ID(@table_name)
    ORDER BY c.column_id
    FOR XML PATH('')
    )

SELECT @sql = 'SELECT SUM(1) AS all_count' +
    @sql + CHAR(10) +
    ' INTO [' + @table_name + '__Col_Counts]' + CHAR(10) +
    ' FROM [' + @table_name + '] WITH (NOLOCK) '
PRINT @sql
--EXEC(@sql)
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

695 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