Solved

query all fields that are completely NULL

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

735 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