?
Solved

query all fields that are completely NULL

Posted on 2015-01-23
5
Medium Priority
?
126 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

764 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