Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to get a list of all columns that have NULL values in all the rows

549855Feb 13 2008 — edited Feb 13 2008
Hi,
I have a big table with about 100 columns.
The table have appx. 1,000,000 rows.
Few of the column have NULL value IN ALL THE RECORDS.
I would like to know, how to get a list of all the columns , that have NULL values in all the rows.
Thank You.

Comments

RadhakrishnaSarma
SQL> create table sample1
  2  as select 1 col1, null col2 from dual
  3  union all
  4  select 2, null from dual
  5  union all
  6  select null, 3 from dual
  7  /

Table created.

SQL> select column_name, num_nulls
  2  from user_tab_col_statistics
  3  where table_name = 'SAMPLE1'
  4  /

no rows selected

SQL> execute dbms_stats.gather_table_stats(user, 'SAMPLE1');

PL/SQL procedure successfully completed.

SQL> select column_name, num_nulls
  2  from user_tab_col_statistics
  3  where table_name = 'SAMPLE1'
  4  /

COLUMN_NAME                     NUM_NULLS
------------------------------ ----------
COL1                                    1
COL2                                    2

SQL> 
Cheers
Sarma.
549855
Hi Sarma,
Thank you for your quick response.
Your solution looks fine , but i dont want to recollect statistics on my production database with noreason.
Could you please suggest another option ?
Thanks.
RPuttagunta
You can write a select which 'counts' each column for number of NON_NULL. Something like:

select 'select count(*), '|| column_name || ' from ' || table_name || ' where ' || column_name || ' is not null;' from user_tab_cols s where s.table_name = 'your_table';

and then:

run the output selects on their own. Wherever the counts are zero, there is your column.

Message was edited by:
RPuttagunta
RadhakrishnaSarma
You can use Dynamic SQL to get the column_names from user_tab_columns and then for each columns execute the null check and get the count.
Your solution looks fine , but i dont want to recollect statistics on my production database with
noreason.
Your approach is not correct. With Production database, you OUGHT to have statistics up-to-date. If you have statistics already available, then skip the collection of statistics and just use my SQL.

Cheers
sarma.
1 - 4
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Mar 12 2008
Added on Feb 13 2008
4 comments
3,114 views