Solved

query all fields that are completely NULL

Posted on 2015-01-23
5
118 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 65

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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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