Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Query to Find Unused Columns

Author  Topic 

zudnic
Starting Member

3 Posts

Posted - 2015-02-05 : 17:55:04
I have an application that has grown organically, and people have asked for new fields and they have been added willy-nilly over time. I want to clean it up and find columns where every row is null - basically unused ones I can remove.

This table is out of control, there are hundreds of columns, so doing this manually is not feasible.

Is there a way to do this?

Thank you!

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2015-02-05 : 19:01:12
1) You could generate some dynamic SQL that would test each column.
2) You could select the count of each column by name; since the null entries don't get included, any column that has a count of zero is unused
declare @T table (
col1 int null,
col2 char null,
colMT date null
);

insert into @t
values
(1, 'A', null),
(null, 'B', null),
(null, 'C', null),
(4, null, null),
(5, 'E', null)

select count(col1), count(col2), count(colMT)
from @T




Those who will not reason, are bigots, those who cannot, are fools, and those who dare not, are slaves. -Lord Byron, poet (1788-1824)
Go to Top of Page

zudnic
Starting Member

3 Posts

Posted - 2015-02-06 : 10:26:13
quote:
Originally posted by Bustaz Kool

1) You could generate some dynamic SQL that would test each column.
2) You could select the count of each column by name; since the null entries don't get included, any column that has a count of zero is unused
declare @T table (
col1 int null,
col2 char null,
colMT date null
);

insert into @t
values
(1, 'A', null),
(null, 'B', null),
(null, 'C', null),
(4, null, null),
(5, 'E', null)

select count(col1), count(col2), count(colMT)
from @T




Those who will not reason, are bigots, those who cannot, are fools, and those who dare not, are slaves. -Lord Byron, poet (1788-1824)

Thank you! I'll give it a shot.
Go to Top of Page

jjourneyy22
Starting Member

3 Posts

Posted - 2015-02-09 : 01:54:05
unspammed
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2015-02-10 : 01:36:14
zudnic, you need to return to database design and investigate why some many NULLs are appearing
Another aspect of growth is to check for indexes which aren't used - use this script http://www.sqlserver-dba.com/2007/06/index-usage.html and this DMV http://www.sqlserver-dba.com/2007/11/sysdm_db_index_.html

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

zudnic
Starting Member

3 Posts

Posted - 2015-02-10 : 11:07:59
quote:
Originally posted by jackv

zudnic, you need to return to database design and investigate why some many NULLs are appearing
Another aspect of growth is to check for indexes which aren't used - use this script http://www.sqlserver-dba.com/2007/06/index-usage.html and this DMV http://www.sqlserver-dba.com/2007/11/sysdm_db_index_.html

Jack Vamvas
--------------------
http://www.sqlserver-dba.com

Thank you! This table is part of a packaged enterprise application. Columns have been added in response to business requests over the years, and some of the added columns never got used. This exercise is part of trying to clean up and implement tighter processes.
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2015-02-11 : 01:47:52
I understand the situation - particuarly if it's a third party product. You may want to review the integrity rules and analyse how they are managing the incidence of the NULL data.
http://www.sqlserver-dba.com/2014/10/where-to-maintain-data-integrity-rules.html

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page
   

- Advertisement -